【问题标题】:Update duplicate rows with duplicated found id使用重复的找到的 id 更新重复的行
【发布时间】:2016-12-29 18:31:09
【问题描述】:

我有这样的表:

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 中做到这一点

【问题讨论】:

    标签: mysql duplicates


    【解决方案1】:

    此查询将返回所有重复的 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, ')')
    

    请查看working fiddle here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 2014-03-08
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      相关资源
      最近更新 更多