UPDATE directoryData
SET CompanyName = CONCAT(CONCAT(CompanyName, ' '), Town)
WHERE CompanyName in (
SELECT CompanyName
FROM directoryData
GROUP BY CompanyName
HAVING count(CompanyName)>1
)
编辑:你是对的,根据MySQL #1093 - 你不能在 FROM 子句中为更新指定目标表“赠品”,如果更新更改了 where 中检查的值,引擎可能会中断(你更新了第一个地铁记录,然后第二个不再匹配 count()>1 子句...)
那么,让我们尝试另一种方式:
UPDATE directoryData
SET CompanyName = (
CASE WHEN (
SELECT count(CompanyName)
FROM directoryData
GROUP BY CompanyName
HAVING count(CompanyName)
)
>1
THEN CONCAT(CONCAT(CompanyName, ' '), Town)
ELSE CompanyName
END
)
我不能尝试,我在这里手工写了这个,希望对你有帮助......
编辑2:
使用两个表进行一次性任务(因为以前的解决方案不适用于 MySql):
UPDATE directoryData
SET CompanyName = CONCAT(CONCAT(CompanyName, ' '), Town)
WHERE CompanyName in (
SELECT CompanyName
FROM directoryData_CLONED
GROUP BY CompanyName
HAVING count(CompanyName)>1
)