为什么字符串在SQLite中匹配NULL?

时间:2013-05-15 23:24:23

标签: sql sqlite

我有两张桌子:

CREATE TABLE Foo(
    id      VARCHAR,
    data    VARCHAR
);

CREATE TABLE Bar(
    id      VARCHAR,
    foo_id  VARCHAR
);

当我运行查询时:SELECT * FROM Foo WHERE id='EB7AB72D7802D1146B72E3F1D761BFA2382BE102' AND id NOT IN (SELECT foo_id FROM Bar)我没有结果。

但是,如果我将查询更改为SELECT * FROM Foo WHERE id='EB7AB72D7802D1146B72E3F1D761BFA2382BE102' AND id NOT IN (SELECT foo_id FROM Bar WHERE foo_id IS NOT NULL),我会得到正确的结果。

表中的数据类似于以下内容:

Foo:

10AF193A2C0465B6DAD69C4E9E2BF86321961434, blah
10AF193A2C0465B6DAD69C4E9E2BF86321961434, bloo
ED724BB8F46596A8F14B891DE4B6BE5ADB0B5903, fdsj
ED724BB8F46596A8F14B891DE4B6BE5ADB0B5903, fdsjio
EB7AB72D7802D1146B72E3F1D761BFA2382BE102, baz

Bar:

0009546DA32EFB77CB29F21E7399A8242866BBC1, 10AF193A2C0465B6DAD69C4E9E2BF86321961434
B732EA74329257213D7076F7672CCD8B1DF8E41B, (null)
7A6752AFAD1F7A887127A38937C15729673EF25F, ED724BB8F46596A8F14B891DE4B6BE5ADB0B5903

1 个答案:

答案 0 :(得分:1)

无法与null进行比较。所以在你的第一个查询中,该语句的布尔结果是'unknown'而不是true或false。

查看此博文,了解详细解释:
http://www.sqlbadpractices.com/using-not-in-operator-with-null-values/