客户表
CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE
1 John Brown 1/1/1965 800-555-1211
2 Cynthia Orange 2/5/1968 800-555-1212
3 Steve White 3/16/1971 800-555-1213
4 Gail Black 800-555-1214
5 Doreen Blue 5/20/1970
6 Fred Brown 1/1/1970 800-555-1215
如果运行查询
select * from customers where customer_id not in (2,3,5,NULL);
我得到一个没有返回行的输出.....请帮我解决这个问题..
最佳答案
您被 SQL 3 值逻辑咬伤了。
对于 customer_id
为 2、3 或 5 的行,WHERE
子句的计算结果为 false,如您所料。
对于其他行,它评估为UNKNOWN
(或NULL
;我不知道Oracle是否区分它们),不是到是的。
如果将 IN
表达式扩展为 (customer_id != 2) AND (customer_id != 3) AND (customer_id != 5) AND (customer_id !=空)
。对于 1、4 或 6 的 customer_id
,前三个子表达式的计算结果为真,如您所料。但最后一个计算结果为未知,因为 NULL
(未知值的标记)可能“真的”是 1、4 或 6。因此,整个表达式具有未知的真值。 SELECT
语句将只返回 WHERE
子句肯定为真而不是未知的行。
您可以通过查询得到您想要的结果。
select * from customers where customer_id not in (2, 3, 5) and customer_id is not null;
但是,您的 customer_id
似乎是一个自动增量列,无论如何都不能为 null。如果是这样,只需写:
select * from customers where customer_id not in (2, 3, 5);
关于sql - 如果列表中的值为空,则 NOT IN 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20209528/