2011-06-10 50 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 

希望这有助于