我需要在
中的查询中显示2个重复行ID | Name | Address
--------------------
1 | John | 123 West Ave
2 | John | 123 West Ave
3 | Peter | 225 North
4 | Sally | 972 Kingsburg
5 | Peter | 225 North
6 | John | 772 Superman
7 | Peter | 882 Batman
,结果将是
ID | Name | Address
---------------------
1 | John | 123 West Ave
2 | John | 123 West Ave
3 | Peter | 225 North
5 | Peter | 225 North
答案 0 :(得分:3)
如果您只想显示name
和address
成对重复的记录,那么您可以尝试以下查询:
SELECT t1.ID, t1.name, t1.address
FROM yourTable t1
INNER JOIN
(
SELECT name, address
FROM yourTable
GROUP BY name, address
HAVING COUNT(*) = 2
) t2
ON t1.name = t2.name AND t1.address = t2.address
如果您希望任何记录显示为两个或更多次,那么您可以将HAVING
子句更改为HAVING COUNT(*) >= 2
。< / p>
答案 1 :(得分:2)
使用聚合函数?
SELECT ID, Name, Address
FROM le_table
GROUP BY Name, Address
HAVING COUNT(ID) > 1
ORDER BY ID
答案 2 :(得分:0)
SELECT *
FROM table
GROUP BY Name, Address
HAVING COUNT(ID) > 1
ORDER BY ID