我有这样的表:
id name description
1 foo
2 bar
3 foo
我想用重复行的 id 更新描述。像这样的东西:
id name description
1 foo duplicate in id (3)
2 bar
3 foo duplicate in id (1)
如何在 Mysql 中执行此操作
最佳答案
此查询将返回所有重复的 ID,并以逗号分隔的共享相同名称的 ID 列表:
select
t1.id,
group_concat(t2.id)
from
tablename t1 inner join tablename t2
on t1.id<>t2.id and t1.name=t2.name
group by
t1.id
此查询将更新描述:
update tablename inner join (
select
t1.id,
group_concat(t2.id) dup
from
tablename t1 inner join tablename t2
on t1.id<>t2.id and t1.name=t2.name
group by
t1.id
) s on tablename.id = s.id
set
description = concat('duplicate id in (', s.dup, ')')
关于mysql - 使用重复的找到的 ID 更新重复的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38561766/