WHERE 子句谓词可以计算为 NULL 吗?

Can a WHERE clause predicate evaluate to NULL?

WHERE 子句 return 可以用 NULL 代替 TRUE 或 FALSE 吗? 根据下面的练习,这是可能的,但我无法想象 returns NULL 的例子,这真的可能吗?

4. Which of the following values can NOT be returned after evaluation of WHERE clause
condition?
A.  UNKNOWN
B.  TRUE
C.  FALSE
D.  NULL
Answer: A. If the result of the condition in WHERE clause is not known, NULL is returned. In all
other scenarios, either TRUE or FALSE is returned.

至于解释原因,请考虑 SQL 语言使用 three-value 逻辑:TRUEFALSENULL。让我们考虑这个订单 table,

如果我们 运行 下面的查询它不会 return 行 CPU 和 Monitor

SELECT * FROM Orders WHERE (qty < 1000 Or qty >= 1000)

在这种情况下,对于 CPU 和监控条件 (qty < 1000 Or qty >= 1000)return 既不是 TRUE 也不是 FALSE。它 returns NULL。因为逻辑上是未知的。因此,WHERE 子句中条件的结果是未知的,它 returned NULL。

你可以考虑这个reference

在 SQL 中,所有逻辑运算符的计算结果均为 TRUE、FALSE 和 UNKNOWN (Oracle docs) in MySQL UNKNOWN result calls NULL (MySQL docs)。

根据 oracle 文档:

"To test for nulls, use only the comparison conditions IS NULL and IS NOT NULL. If you use any other condition with nulls and the result depends on the value of the null, then the result is UNKNOWN."

因此在评估后只能返回 TRUE、FALSE 和 UNKNOWN。

关于您的问题:

"Can a WHERE clause return NULL instead of TRUE or FALSE?"

严格来说在 Oracle 中 - NO 因为这样的结果称为 UNKNOWN。

但总的来说,UNKNOWN 和 NULL 的含义在这种情况下是等价的,只是同一事物的不同名称。 因此下面的 SQL 示例 (a.a >= all) 被评估为 UNKNOWN。

with table_a as (
select null as a from dual
union all 
select 10 as a from dual
union all 
select 5 as a from dual),
table_b as (
select null as a from dual
union all 
select 10 as a from dual
union all 
select 5 as a from dual)

select * from table_a a where a.a >= all(select a from table_b b)

Not even a NULL can be equal to NULL.

  1. NULL的正确理解是它不是一个值。不是 “这是一个 NULL 值”但是“这个 NULL 不是一个值。”一切 要么是一个值,要么不是。
  2. 当某物是一个值时,它是“1”或“你好”或“绿色”或 “5.00 美元”等——但当某物不是价值时,它就不是 任何东西。
  3. SQL 通过特殊的 non-value NULL 表示“this has no value”。 当有人说“NULL 值”时,人们在心理上应该不同意, 因为没有这样的东西。 NULL 是完全的、完全的缺失 任何价值。 强调文本

一个Non-Technical方面

If you ask two girls, how old they are? may be you would hear them to refuse to answer your question, Both girls are giving you NULL as age and this doesn't mean both have similar age. So there is nothing can be equal to null.

SELECT 0 IS NULL , 0 IS NOT NULL , '' IS NULL , '' IS NOT NULL, NULL != NULL, NULL = NULL, NULL != '', NULL = ''