使用此查询:
SELECT ARCustomerCode,
count(ARCustomerCode)
FROM customers
group by ARCustomerCode
having count(ARCustomerCode) > 1;
我能够识别 ARCustomerCode 不唯一的行数。如何通过附加唯一行 ID 来更新每个重复的 ARCustomerCode 字段?
欣赏洞察力!
最佳答案
您可以利用ROW_NUMBER
窗口函数为每个重复的“ARCustomerCode”值分配排名,然后仅更新每个代码的排名 > 1(所有重复项)的记录。
WITH cte AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY ARCustomerCode ORDER BY <row_id_col>) AS rn
FROM customers
)
UPDATE customers
INNER JOIN cte
ON customers.<row_id_col> = cte.<row_id_col>
SET customers.ARCustomerCode = CONCAT(customers.ARCustomerCode, customers.<row_id_col>)
WHERE rn > 1
关于mysql - 如何在 MySQL 中查找重复值和更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74799585/