考虑以下情况,
No. Name Tag
1 ABC X1
2 DEF x1
3 GHI x1
4 JKL x2
5 MNO x3
6 PQR x4
7 PQR x4
8 STU x4
9 VWX X5
10 YZI x5
现在我需要找到具有相同名称和标签的重复项。
理想情况下,结果应为
No. Name Tag
6 PQR x4
7 PQR x4
答案 0 :(得分:3)
这将适用于大多数RDBMS,因为您尚未指定。
SELECT *
FROM TableName a
WHERE EXISTS
(
SELECT 1
FROM TableName b
WHERE a.name = b.Name AND
a.Tag = b.Tag
GROUP BY Name, Tag
HAVING COUNT(*) > 1
)
答案 1 :(得分:1)
在MySQL中,您可以这样使用棘手的对where
子句:
SELECT * FROM t
WHERE (name, tag) IN (
SELECT name, tag FROM t
GROUP BY name, tag
HAVING COUNT(*) > 1
)
小提琴here。
答案 2 :(得分:0)
请尝试:
SELECT b.*
FROM (
SELECT Name, Tag
FROM YourTable
GROUP BY Name, Tag
HAVING COUNT(*) > 1
)x inner join YourTable b on x.Name=b.Name and x.Tag=b.Tag