且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更新具有重复找到的ID的重复行

更新时间:2023-12-01 11:35:40

此查询将返回所有重复的ids,并以逗号分隔的ids名称共享相同的名称:

This query will return all duplicated ids with a comma separated list of ids that share the same name:

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, ')')

请看一个工作小提琴在这里。