2012-10-02 125 views
24

可能重复:
Mysql Duplicate Rows (Duplicate detected using 2 columns)如何根据MySQL中的多个字段查找重复行?

在MySQL数据库中我有很多行。例如:

id | title | time | domain 
32 title1 12:30 domain1.com 
33 title1 12:30 domain2.com 
34 title2 14:20 domain1.com 
35 title3 14:30 domain2.com 
36 title1 12:30 domain55.com 

我怎么能基于标题和时间从数据库中选择行?重复的域或ID是不重要的,只有其他两个字段。

我希望能够检索第32,33和36行,因为它们具有相同的标题和相同的时间。

我不希望输入标题或时间,我希望查询返回在这两个字段中找到的“重复”匹配的所有字段,无论这两个字段是否只有两个或五十个。这样,我可以通过并编辑或删除一些重复项。

+0

能否请您接受的答案,如果你喜欢它吗? – kasi

回答

28
select distinct id, title, time 
    from table t1 
where exists (select * 
       from table t2 
       where t2.id <> t1.id 
        and t2.title = t1.title 
        and t2.time = t1.time 
       ) 
+3

这实际上回答了这个问题,返回所有三个重复的行,而[其他答案](http://stackoverflow.com/a/12691631/283991)只返回一行。 – mwotton

58

这里是你想要

SELECT title, time 
    FROM table 
GROUP BY title, time 
    HAVING count(*) > 1 
相关问题