我想在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 COUNT(*) c,title FROM `data` GROUP BY title HAVING c > 1;

其他回答

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

SELECT *
FROM list AS outer
WHERE (SELECT COUNT(*)
        FROM list AS inner
        WHERE inner.address = outer.address) > 1;
select * from table_name t1 inner join (select distinct <attribute list> from table_name as temp)t2 where t1.attribute_name = t2.attribute_name

对于你的桌子来说,应该是这样的

select * from list l1 inner join (select distinct address from list as list2)l2 where l1.address=l2.address

这个查询将为您提供列表表中所有不同的地址条目…我不知道这将如何工作,如果你有任何主键值的名称等。

    SELECT *
    FROM (SELECT  address, COUNT(id) AS cnt
    FROM list
    GROUP BY address
    HAVING ( COUNT(id) > 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替换为字段名

 SELECT firstname, lastname, address FROM list
 WHERE 
 Address in 
 (SELECT address FROM list
 GROUP BY address
 HAVING count(*) > 1)