IN
和
EXITS
NOT IN 语句实质上等同于使用 = 比较每一值,如果测试为 FALSE 或者 NULL ,结果为比较失败。例如
select 'true' from dual where 1 not in (null,2);
select 'true' from dual where 1 != null and 1 != 2;
select 'true' from dual where (1,2) not in ((2,3),(2,null));
select 'true' from dual where (1,null) not in ((1,2),(2,3));
这些查询不会返回任何一行。第二个查询语句更为明显,即 1 != null ,所以整个 WHERE 都为 false 。然而这些查询语句可变为:
select 'true' from dual where 1 not in (2,3);
select 'true' from dual where 1 != 2 and 1 != 3;
你也可以使用 NOT IN 查询,只要你保证返回的值不会出现 NULL 值:
select ename from emp where empno not in (select mgr from emp where mgr is not null );
select ename from emp where empno not in (select nvl(mgr,0) from emp);
通过理解 IN , EXISTS , NOT IN ,以及 NOT EXISTS 之间的差别,当 NULL 出现在任一子查询中时,你可以避免一些常见的问题
NOT IN 语句实质上等同于使用 = 比较每一值,如果测试为 FALSE 或者 NULL ,结果为比较失败。例如
select 'true' from dual where 1 not in (null,2);
select 'true' from dual where 1 != null and 1 != 2;
select 'true' from dual where (1,2) not in ((2,3),(2,null));
select 'true' from dual where (1,null) not in ((1,2),(2,3));
这些查询不会返回任何一行。第二个查询语句更为明显,即 1 != null ,所以整个 WHERE 都为 false 。然而这些查询语句可变为:
select 'true' from dual where 1 not in (2,3);
select 'true' from dual where 1 != 2 and 1 != 3;
你也可以使用 NOT IN 查询,只要你保证返回的值不会出现 NULL 值:
select ename from emp where empno not in (select mgr from emp where mgr is not null );
select ename from emp where empno not in (select nvl(mgr,0) from emp);
通过理解 IN , EXISTS , NOT IN ,以及 NOT EXISTS 之间的差别,当 NULL 出现在任一子查询中时,你可以避免一些常见的问题
not in
逻辑上不完全等同于
not exists
请注意 not in 逻辑上不完全等同于 not exists ,如果你误用了 not in ,小心你的程序存在致命的 BUG :
请看下面的例子:
create table t1 (c1 number,c2 number);
create table t2 (c1 number,c2 number);
insert into t1 values (1,2);
insert into t1 values (1,3);
insert into t2 values (1,2);
insert into t2 values (1,null);
select * from t1 where c2 not in (select c2 from t2);
no rows found
select * from t1 where not exists (select 1 from t2 where t1.c2=t2.c2);
c1 c2
1 3
正如所看到的, not in 出现了不期望的结果集,存在逻辑错误。如果看一下上述两个 select 语句的执行计划,也会不同。后者使用了 hash_aj 。
因此,请尽量不要使用 not in( 它会调用子查询 ) ,而尽量使用 not exists( 它会调用关联子查询 ) 。如果子查询中返回的任意一条记录含有空值,则查询将不返回任何记录,正如上面例子所示。
除非子查询字段有非空限制,这时可以使用 not in , 并且也可以通过提示让它使用 hasg_aj 或 merge_aj 连接。
请注意 not in 逻辑上不完全等同于 not exists ,如果你误用了 not in ,小心你的程序存在致命的 BUG :
请看下面的例子:
create table t1 (c1 number,c2 number);
create table t2 (c1 number,c2 number);
insert into t1 values (1,2);
insert into t1 values (1,3);
insert into t2 values (1,2);
insert into t2 values (1,null);
select * from t1 where c2 not in (select c2 from t2);
no rows found
select * from t1 where not exists (select 1 from t2 where t1.c2=t2.c2);
c1 c2
1 3
正如所看到的, not in 出现了不期望的结果集,存在逻辑错误。如果看一下上述两个 select 语句的执行计划,也会不同。后者使用了 hash_aj 。
因此,请尽量不要使用 not in( 它会调用子查询 ) ,而尽量使用 not exists( 它会调用关联子查询 ) 。如果子查询中返回的任意一条记录含有空值,则查询将不返回任何记录,正如上面例子所示。
除非子查询字段有非空限制,这时可以使用 not in , 并且也可以通过提示让它使用 hasg_aj 或 merge_aj 连接。