0

请帮助我使用 MySql 脚本,我遇到了一个错误。我有一个包含 p_id 和 prod_skuid 字段的表(tbl_products),我想将所有重复的 prod_skuid 更新到我的 tbl_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

我设法仅使用没有更新脚本的嵌套选择来显示和更改 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

错误: Mysql 错误信息

4

1 回答 1

0

无论如何,我现在得到了答案。我已经运行了很多测试,现在它是:

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
于 2018-05-11T04:07:17.553 回答