我有一张下表,其中包含以下数据:
表
现在,我想获取所有在B列中不具有值5的用户(仅与众不同)。我的意思是user1在某行中具有值5,然后关闭所有用户1行。
应产生以下结果: user2(因为值为null) user3(无值5)
我该怎么做?
答案 0 :(得分:2)
也许最简单的方法是由用户聚合:
SELECT ColumnA
FROM yourTable
GROUP BY ColumnA
HAVING COUNT(CASE WHEN ColumnB = 5 THEN 1 END) = 0;
答案 1 :(得分:2)
一种方法是聚合:
select columnA
from t
group by columnA
having sum(case when columnB = 5 then 1 else 0 end) = 0;
答案 2 :(得分:1)
您可以通过减号运算符执行此操作
SELECT distinct colA
FROM have
WHERE colB not in(5)
MINUS
SELECT distinct colA
FROM have
WHERE colB=5;
答案 3 :(得分:1)
使用NOT EXISTS
可以获得结果
SELECT DISTINCT T1.ColumnA
FROM TableName T1
WHERE NOT EXISTS (
SELECT * FROM TableName T2 WHERE T2.ColumnA = T1.ColumnA AND T2.ColumnB <> 5
)
答案 4 :(得分:1)
另一种方法-
SELECT DISTINCT T1.ColumnA
FROM TableName T1
WHERE T1.ColumnA NOT IN
(
SELECT T2.ColumnA FROM TableName T2 WHERE T2.ColumnB = 5
)