问题描述
sql1:从不存在的t1中选择*(从t2中选择a,其中t2.a = t1.a);
sql2:从t1中选择*,其中t1.a不存在(从t2中选择a,其中t2.a不为空);
我认为sql1与sql2相同,它们将重写为anti join,对吗?
解决方法
如果您有NULL
值,则这两个查询将给出不同的结果。
Oracle中的示例:
create table t1 (a integer);
insert into t1 (a) values (1);
insert into t1 (a) values (2);
insert into t1 (a) values (null);
commit;
create table t2 (a integer);
insert into t2 (a) values (1);
insert into t2 (a) values (3);
insert into t2 (a) values (null);
commit;
set null "{NULL}"
prompt First Query...
select * from t1 where not exists (select a from t2 where t2.a = t1.a);
prompt Second Query...
select * from t1 where t1.a not in (select a from t2 where t2.a is not null);
输出:
First Query...
A
----------
2
{NULL}
2 rows selected.
Second Query...
A
----------
2
1 row selected.