我想在MySQL数据库中提取重复记录。这可以通过以下方式完成:
SELECT address, count(id) as cnt FROM list GROUP BY address HAVING cnt > 1
结果是:
100 MAIN ST 2
我想将其拉出,以使其显示重复的每一行。就像是:
JIM JONES 100 MAIN ST JOHN SMITH 100 MAIN ST
关于如何做到这一点有什么想法?我试图避免做第一个,然后在代码中用第二个查询查找重复项。
关键是重写此查询,以便可以将其用作子查询。
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;