我正在尝试查找序列中没有特定值的记录:
客户表:
例如,我需要找到所有没有"价值F"记录在案。
SELECT * FROM Customer
WHERE VALUE NOT IN 'Value F'
也会返回值A,B,C等
答案 0 :(得分:1)
SELECT *
FROM Customer c
WHERE NOT EXISTS ( SELECT 1
FROM Customer c2
WHERE c2.CustomerID = c.CustomerID
AND VALUE = 'Value F' )
NOT EXISTS可以在这里使用。
你的NOT IN声明需要看起来像这样。
SELECT *
FROM Customer
WHERE CustomerId NOT IN (SELECT CustomerID
FROM Customer
WHERE VALUE = 'Value F')
答案 1 :(得分:1)
使用group by
和having
:
select customerid
from customers
group by customerid
having sum(case when value = 'Value F' then 1 else 0 end) = 0;