首页 > 解决方案 > “不在哪里” - 最简单的查询中的奇怪返回

问题描述

我正在碰壁如何解释这一点:

select * from (
  select '123' id1 from dual
) src
where src.id1 not in (select id1 from table1)
;

没有给我行

select * from (
  select '123' id1 from dual
) src
where src.id1 in (select id1 from table1)
;

也没有给我任何行。

在这两种情况下这怎么可能?

另一方面,如果不存在,则按预期工作。表 1 不为空。实际上,是的 - table1 既有空值也有非空值。为什么这会使相反的条件错误?

编辑:不完全重复,因为列出的类似问题是为什么结果不同,而在这里 - 为什么在相反条件下结果相同 - 这可能会令人困惑。

标签: oracle

解决方案


It looks like your list of ids to test against contains NULL values.

Comparisons with NULL always return false. Even WHERE NULL = NULL is false (same as WHERE NULL != NULL). You'd need to use IS NULL.

In your case, you probably want select id1 from table1 where id1 is not null


推荐阅读