使用嵌套select更新mysql中的重复值

eeq64g8w  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(256)

请帮助我的mysql脚本,我赶上了一个错误。我有一个表(tbl\u products),其中包含p\u id和prod\u skuid字段,我想将所有重复的prod\u skuid更新到我的tbl\u products中。
以下是我的mysql脚本:

UPDATE tbl_product t1 SET t1.prod_skuid = t.prod_skuid  
(
SELECT p_id, prod_skuid from 
(SELECT p_id,if(@row = prod_skuid, @count, @count := 1),
 CONCAT(prod_skuid, '', @count) as prod_skuid,
 @row := prod_skuid,
 @count := @count + 1
 from tbl_product JOIN (SELECT @count := 0, @row := "") as t
 WHERE prod_skuid IN(SELECT prod_skuid from tbl_product group by prod_skuid having count(prod_skuid) > 1)
) as final                          
) as t  
WHERE t1.p_id = t.p_id

我只使用嵌套的select而不使用update scrpit来显示和更改prod\u skuid,但是当我放入update脚本时,它表示我的代码有错误:

SELECT p_id, prod_skuid from 
(SELECT p_id,if(@row = prod_skuid, @count, @count := 1),
 CONCAT(prod_skuid, '', @count) as prod_skuid,
 @row := prod_skuid,
 @count := @count + 1
 from tbl_product JOIN (SELECT @count := 0, @row := "") as t
 WHERE prod_skuid IN(SELECT prod_skuid from tbl_product group by prod_skuid having count(prod_skuid) > 1)
) as final

错误:mysql错误消息

bjg7j2ky

bjg7j2ky1#

不管怎样,我现在得到了答案。我做了这么多测试,现在是:

UPDATE tbl_product t1, 
( SELECT p_id as unique_id, new_name FROM
( SELECT p_id, IF(@ROW = prod_skuid, @COUNT, @COUNT := 1), 
CONCAT(prod_skuid, ' - ', @COUNT) AS new_name, 
@ROW := prod_skuid, 
@COUNT := @COUNT + 1 
FROM tbl_product JOIN (SELECT @COUNT := 0, @ROW := "") AS t 
WHERE prod_skuid IN(SELECT prod_skuid FROM tbl_product 
GROUP BY prod_skuid HAVING COUNT(prod_skuid) > 1) ) AS temp_test ) 
as testing SET t1.prod_skuid = testing.new_name where t1.p_id = testing.unique_id

相关问题