我试图使用Oracle DB从一个表中获取不在另一个表中的条目。
以下查询不返回任何记录:
select distinct FOO from TABLE_A where FOO not in (select distinct FOO from TABLE_B)但下列返回记录与预期相同:
(select distinct FOO from TABLE_A) minus (select distinct FOO from TABLE_B)为什么第一个查询不能工作?
我检查过的东西:
发布于 2017-10-18 10:41:56
将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了。
https://stackoverflow.com/questions/46808075
复制相似问题