Tabls是
ID Count
1 30
2 30
3 10
4 15
5 10
6 25
我想要查询哪个会给我
4 15
6 25
结果
答案 0 :(得分:3)
您可以使用NOT EXISTS
:
SELECT ID, Count
FROM dbo.TableName t1
WHERE NOT EXISTS
(
SELECT 1 FROM dbo.TableName t2
WHERE t1.ID <> t2.ID AND t1.Count = t2.Count
)
答案 1 :(得分:1)
以下内容应选择您想要的内容:
SELECT t.ID, t.[Count]
FROM Table t
WHERE
(SELECT COUNT(*) FROM Table t1 WHERE t1.[Count] = t.[Count]) = 1
请注意,您应该在Table.[Count]
上有一个索引。
答案 2 :(得分:1)
您也可以使用分组声明
SELECT MIN(ID), Count
FROM Table
GROUP BY Count
HAVING COUNT(*) = 1
答案 3 :(得分:0)
将HAVING与COUNT DISTINCT一起使用,以限制结果:
SELECT [Id], [Count]
FROM MyTable
WHERE [Count] IN (
SELECT [Count]
FROM MyTable
GROUP BY [Count]
HAVING COUNT(DISTINCT [Count]) = 1
)