How do you find unlisted IDs when an exclusion list contains NULL?

NOT EXISTS asks whether an equal match exists. An unrelated NULL in the exclusion list does not turn other comparisons into unknown membership results.

SQLite example and interpretation

A list containing two and an unrecorded ID still lets candidate one through. Decide separately whether anonymous candidates should qualify.

WITH s(id) AS (VALUES(1),(2)), b(id) AS (VALUES(2),(NULL)) SELECT id FROM s WHERE NOT EXISTS(SELECT 1 FROM b WHERE b.id=s.id);

Result [[1]]

中文

黑名单含空值时,怎样找出未命中编号?

NOT EXISTS 根据是否有相等的匹配行筛选,不会因右侧另有一个 NULL 就把所有非匹配项变成未知。

SQLite 示例与适用条件

名单含 2 和一个未录入的编号,候选 1 仍应通过。若候选自身也可能为空,应另定是否接受匿名记录。

WITH s(id) AS (VALUES(1),(2)), b(id) AS (VALUES(2),(NULL)) SELECT id FROM s WHERE NOT EXISTS(SELECT 1 FROM b WHERE b.id=s.id);

结果 [[1]]

References / 参考来源

Keep reading / 继续阅读