我想提取 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;
SELECT date FROM logs group by date having count(*) >= 2
为什么不将 INNER JOIN 本身与桌子相连?
SELECT a.firstname, a.lastname, a.address
FROM list a
INNER JOIN list b ON a.address = b.address
WHERE a.id <> b.id
如果地址可以存在两次以上,则需要 DISTINCT。