我正在尝试使用With
从一个表中获取不在另一个表中的条目。
以下查询不会返回任何记录:
Oracle DB
但是以下内容按预期返回记录:
select distinct FOO from TABLE_A where FOO not in (select distinct FOO from TABLE_B)
为什么第一个查询不起作用?
我检查过的事情:
答案 0 :(得分:2)
将not exists
与子查询一起使用:
select distinct s.FOO
from TABLE_A a
where not exists (select 1 from table_b b where b.foo = a.foo);
当任何 b.foo
为NULL
时,所有行都会被过滤掉。在这种情况下,not in
返回FALSE或NULL。条件永远不会返回TRUE。
not exists
具有预期的语义。
我应该在使用in
时添加(如果您仍想使用not in
),则子查询中不需要select distinct
。