SQL查询、计数和分组依据

Translate

如果我有这样的数据:

+---+----+
|Key|Name|
+---+----+
|1  |Dan |
+---+----+
|2  |Tom |
+---+----+
|3  |Jon |
+---+----+
|4  |Tom |
+---+----+
|5  |Sam |
+---+----+
|6  |Dan |
+---+----+

什么是SQL查询将记录带回哪里Name重复两次或更多次?

所以我想要的结果是

 +---+
 |Tom|
 +---+
 |Dan|
 +---+
This question and all comments follow the "Attribution Required."

所有的回答

Translate

再简单不过了...

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)
来源
Translate
select name from table group by name having count(name) > 1
来源
Translate

这也可以通过将表与其自身连接来实现,

SELECT DISTINCT t1.name
FROM    tbl t1
        INNER JOIN tbl t2
        ON      t1.name = t2.name
WHERE   t1.key         != t2.key;
来源
上一个问题:
有人在用Foggbugz吗?