我想在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
有什么想法可以做到吗?我试图避免做第一个,然后在代码中用第二个查询查找重复。
这里的大多数答案不适用于有多个重复结果和/或有多个列要检查重复的情况。当你在这种情况下,你可以使用这个查询来获得所有重复的id:
SELECT address, email, COUNT(*) AS QUANTITY_DUPLICATES, GROUP_CONCAT(id) AS ID_DUPLICATES
FROM list
GROUP BY address, email
HAVING COUNT(*)>1;
如果希望将每个结果作为一行列出,则需要更复杂的查询。这是我发现有效的方法:
CREATE TEMPORARY TABLE IF NOT EXISTS temptable AS (
SELECT GROUP_CONCAT(id) AS ID_DUPLICATES
FROM list
GROUP BY address, email
HAVING COUNT(*)>1
);
SELECT d.*
FROM list AS d, temptable AS t
WHERE FIND_IN_SET(d.id, t.ID_DUPLICATES)
ORDER BY d.id;