为什么 select IN 在有 null 时不能正常工作
Why select IN doesnt work properly when there are nulls
为什么这会在 EXASOL DBMS 中返回一条记录
SELECT a, b FROM (SELECT NULL AS a, 'b' AS b) t
WHERE (a, b) NOT IN (SELECT NULL, 'b')
?
谢谢,拉斐尔。
因为(NULL = NULL) <> True
create table three(
one integer
);
INSERT INTO three(one) VALUES(1),(2),(NULL);
SELECT one.one AS one
, two.one AS two
, one.one = two.one AS diff
FROM three one
CROSS JOIN three two
;
结果:
CREATE TABLE
INSERT 0 3
one | two | diff
-----+-----+------
1 | 1 | t
1 | 2 | f
1 | |
2 | 1 | f
2 | 2 | t
2 | |
| 1 |
| 2 |
| |
(9 rows)
因此,将任何内容与 NULL 进行比较都会得到 NULL,不是 对或错。
在您的例子中,您比较的是元组,而不是标量。对于元组,情况更糟:如果其中一个元组中的任何一个元素为 NULL,则比较结果既不是 True 也不是 False。
为什么这会在 EXASOL DBMS 中返回一条记录
SELECT a, b FROM (SELECT NULL AS a, 'b' AS b) t
WHERE (a, b) NOT IN (SELECT NULL, 'b')
?
谢谢,拉斐尔。
因为(NULL = NULL) <> True
create table three(
one integer
);
INSERT INTO three(one) VALUES(1),(2),(NULL);
SELECT one.one AS one
, two.one AS two
, one.one = two.one AS diff
FROM three one
CROSS JOIN three two
;
结果:
CREATE TABLE
INSERT 0 3
one | two | diff
-----+-----+------
1 | 1 | t
1 | 2 | f
1 | |
2 | 1 | f
2 | 2 | t
2 | |
| 1 |
| 2 |
| |
(9 rows)
因此,将任何内容与 NULL 进行比较都会得到 NULL,不是 对或错。
在您的例子中,您比较的是元组,而不是标量。对于元组,情况更糟:如果其中一个元组中的任何一个元素为 NULL,则比较结果既不是 True 也不是 False。