检查记录序列是否包含特定值

时间:2016-08-03 13:58:19

标签: sql sql-server

我正在尝试查找序列中没有特定值的记录:

客户表:

enter image description here

例如,我需要找到所有没有"价值F"记录在案。

SELECT * FROM Customer
WHERE VALUE NOT IN 'Value F'  

也会返回值A,B,C等

2 个答案:

答案 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 byhaving

select customerid
from customers
group by customerid
having sum(case when value = 'Value F' then 1 else 0 end) = 0;