2011-06-10 46 views
0
select * from table1 where destination in ('chi','usa','ind') 

理想情況下,我的結果集應該是3行。每個目的地一行。SQL - 找到重複項

但在我的結果有重複。

chi 
usa 
ind 
usa 

我想找到哪個目的地是重複的。 我有數百個目的地,我想刪除重複的記錄。 我不是在尋找獨特的。我正在尋找重複的。

回答

7

使用由基,具有鮮明的

select destination from table1 
where destination in ('chi','usa','ind') 
group by destination 
having count(*)>1 

,而不是如果你想刪除這些並保留一個,就有點凌亂。這可能是最短的方式,但它有點破解。

delete from destination where id not in (
    select max(id) from table1 
     where destination in ('chi','usa','ind') 
     group by destination 
    ) 
+0

由數秒鐘毆打,+1 – Dalen 2011-06-10 22:20:52

+0

感謝。這樣可行。現在不刪除。找到 – zod 2011-06-10 22:27:38

1

這會給你重複計數,通過愚弄次數進行排序說明:

SELECT CountryAbbr, 
     COUNT(*) AS DuplicateCount 
FROM YourTable 
GROUP BY CountryAbbr 
HAVING COUNT(*) > 1 
Order By 2 DESC 
0

在其結尾......應該給你兩列添加組,上是國家,另一個是列表中的次數。

所以..

select * from table1 where destination in ('chi','usa','ind') group by destination 

希望這有助於