零零乙 2009-05-12 18:24 采纳率: 33.3%
浏览 337
已采纳

在 MySQL 中找到重复的记录

I want to pull out duplicate records in a MySQL Database. This can be done with:

SELECT address, count(id) as cnt FROM list
GROUP BY address HAVING cnt > 1

Which results in:

100 MAIN ST    2

I would like to pull it so that it shows each row that is a duplicate. Something like:

JIM    JONES    100 MAIN ST
JOHN   SMITH    100 MAIN ST

Any thoughts on how this can be done? I'm trying to avoid doing the first one then looking up the duplicates with a second query in the code.

转载于:https://stackoverflow.com/questions/854128/find-duplicate-records-in-mysql

  • 写回答

22条回答 默认 最新

  • 10.24 2009-05-12 18:33
    关注

    The key is to rewrite this query so that it can be used as a subquery.

    SELECT firstname, 
       lastname, 
       list.address 
    FROM list
       INNER JOIN (SELECT address
                   FROM   list
                   GROUP  BY address
                   HAVING COUNT(id) > 1) dup
               ON list.address = dup.address;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(21条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突