NOT IN运算符问题Oracle

Ram*_*Ram 5 sql oracle plsql

这是我的查询:

Select a.* from Table1 a, Table2 b
Where 
a.tid=b.tid and 
b.createddate=(Select max(createddate) from Table2) and
a.tid not in (Select distinct tid from Table3);
Run Code Online (Sandbox Code Playgroud)

问题是我知道这应该返回一些有效的输出,但事实并非如此.问题是a.tid中的最后一行不在(从表3中选择不同的tid); 如果我使用硬编码值(''T001','T002','T003','T004')替换Table3中的select distinct tid,那么它可以正常工作并返回数据.

怎么了?我错过了什么吗?请帮忙.

Cra*_*aig 15

试试这个:

Select a.* from Table1 a, Table2 b
Where 
a.tid=b.tid and 
b.createddate=(Select max(createddate) from Table2) and
a.tid not in (Select tid from Table3 where tid is not null);
Run Code Online (Sandbox Code Playgroud)

正如所提到的注释中的所有人一样,如果table3中的tid至少有一行具有空值,则不会返回任何行.这是因为Oracle null就像是在说"我不知道这个值是什么".Oracle无法肯定地说您正在搜索的值肯定不在您的子选择中,因为它不知道这个"未知"值实际上是什么.此外,文档说它的工作方式是这样的:http: //docs.oracle.com/cd/B28359_01/server.111/b28286/conditions013.htm

另一种选择是将查询编写为:

Select a.* from Table1 a, Table2 b
Where 
a.tid=b.tid and 
b.createddate=(Select max(createddate) from Table2) and
not exists (Select null from Table3 t3 where t3.tid = a.tid);
Run Code Online (Sandbox Code Playgroud)

空值的处理是不存在与不存在之间的主要区别之一.