【问题标题】:Get all the records that are duplicates not just the list of them Mysql获取所有重复的记录,而不仅仅是它们的列表 Mysql
【发布时间】:2016-04-11 19:16:07
【问题描述】:

我可以的

Select FieldA,FieldB,FieldC,Count(*) from TableA Group By FieldA,FieldB having count(*)>1

这将为我提供所有 FieldA、FieldB 重复项的列表,并为每个重复项进行计数。我需要的是该子集中的所有记录。如果特定的 FieldA、FieldB 组合的计数为 3,我需要查看所有 3 条记录。我尝试了各种连接都无济于事。

【问题讨论】:

    标签: mysql duplicates


    【解决方案1】:

    只需将您的表 JOIN 转换为包含所有 FieldA,FieldB 对以及每对相应计数的派生表:

    select t1.*, t2.cnt
    from TableA t1
    join (
      Select FieldA, FieldB, Count(*) as cnt
      from TableA 
      Group By FieldA, FieldB 
      having count(*) > 1
    ) as t2 on t1.FieldA = t2.FieldA and t1.FieldB = t2.FieldB
    

    【讨论】:

      【解决方案2】:
      select a1.* 
      from TableA a1
      join
      (
         Select FieldA, FieldB
         from TableA 
         Group By FieldA, FieldB 
         having count(*) > 1
      ) a2 on a1.FieldA = a2.FieldA
          and a1.FieldB = a2.FieldB
      

      在分组的结果上加入同一张表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-10
        • 2019-06-29
        • 1970-01-01
        • 1970-01-01
        • 2016-03-04
        • 2014-01-14
        • 1970-01-01
        相关资源
        最近更新 更多