If I have data like this:
如果我有这样的数据:
+---+----+
|Key|Name|
+---+----+
|1 |Dan |
+---+----+
|2 |Tom |
+---+----+
|3 |Jon |
+---+----+
|4 |Tom |
+---+----+
|5 |Sam |
+---+----+
|6 |Dan |
+---+----+
What is the SQL query to bring back the records where Name
is repeated 2 or more times?
将Name重复2次或更多次的记录带回来的SQL查询是什么?
So the result I would want is
所以我想要的结果是
+---+
|Tom|
+---+
|Dan|
+---+
3 个解决方案
#1
35
Couldn't be simpler...
不能简单......
Select
Name,
Count(Name) As Count
From
Table
Group By
Name
Having
Count(Name) > 1
Order By
Count(Name) Desc
This could also be extended to delete duplicates:
这也可以扩展为删除重复项:
Delete
From
Table
Where
Key In (
Select
Max(Key)
From
Table
Group By
Name
Having
Count(Name) > 1)
#2
4
select name from table group by name having count(name) > 1
#3
3
This could also be accomplished by joining the table with itself,
这也可以通过将表连接到自身来实现,
SELECT DISTINCT t1.name
FROM tbl t1
INNER JOIN tbl t2
ON t1.name = t2.name
WHERE t1.key != t2.key;
#1
35
Couldn't be simpler...
不能简单......
Select
Name,
Count(Name) As Count
From
Table
Group By
Name
Having
Count(Name) > 1
Order By
Count(Name) Desc
This could also be extended to delete duplicates:
这也可以扩展为删除重复项:
Delete
From
Table
Where
Key In (
Select
Max(Key)
From
Table
Group By
Name
Having
Count(Name) > 1)
#2
4
select name from table group by name having count(name) > 1
#3
3
This could also be accomplished by joining the table with itself,
这也可以通过将表连接到自身来实现,
SELECT DISTINCT t1.name
FROM tbl t1
INNER JOIN tbl t2
ON t1.name = t2.name
WHERE t1.key != t2.key;