2012-10-02 127 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 
相關問題