我想在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 * FROM mytable
WHERE id IN (
  SELECT id FROM mytable
  GROUP BY column1, column2, column3
  HAVING count(*) > 1
)

其他回答

不是很有效,但应该可以工作:

SELECT *
FROM list AS outer
WHERE (SELECT COUNT(*)
        FROM list AS inner
        WHERE inner.address = outer.address) > 1;

SELECT * FROM booking WHERE DATE(created_at) = '2022-01-11' 和代码在( 从预订中选择代码 按代码分组 have COUNT(code) > )由id DESC订购

SELECT id, count(*) as c  
 FROM 'list'
GROUP BY id HAVING c > 1

这将返回id和该id重复的次数,或者什么都没有,在这种情况下,您将不会有重复的id。

通过(例如:address)更改组中的id,它将返回一个地址由第一个找到的id与该地址重复标识的次数。

SELECT id, count(*) as c  
 FROM 'list'
GROUP BY address HAVING c > 1

我希望这能有所帮助。喜欢。)

SELECT t.*,(select count(*) from city as tt where tt.name=t.name) as count FROM `city` as t where (select count(*) from city as tt where tt.name=t.name) > 1 order by count desc

用你的表格替换城市。 将name替换为字段名

这里的大多数答案不适用于有多个重复结果和/或有多个列要检查重复的情况。当你在这种情况下,你可以使用这个查询来获得所有重复的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;