25

如果我有这样的数据:

钥匙 姓名
1
2 汤姆
3 乔恩
4 汤姆
5 山姆
6

什么是 SQL 查询来带回Name重复 2 次或更多次的记录?

所以我想要的结果是

汤姆
4

3 回答 3

39

再简单不过了……

Select Name, Count(Name) As Count 
    From Table
    Group By Name
    Having Count(Name) > 1
    Order By Count(Name) Desc

这也可以扩展到删除重复项:

Delete From Table
Where Key In (
    Select Max(Key)
        From Table
        Group By Name
        Having Count(Name) > 1
    )
于 2008-08-06T09:02:36.907 回答
4
select name from table group by name having count(name) > 1
于 2008-08-17T04:36:53.657 回答
3

这也可以通过将表与自身连接来完成,

SELECT DISTINCT t1.name
FROM    tbl t1
        INNER JOIN tbl t2
        ON      t1.name = t2.name
WHERE   t1.key         != t2.key;
于 2008-08-10T01:31:05.703 回答