sql1: select * from t1 where not exists (select a from t2 where t2.a = t1.a);
sql2: select * from t1 where t1.a not in (select a from t2 where t2.a is not null);
我认为sql1和sql2一样,他们会重写为anti join,对吧?
sql1: select * from t1 where not exists (select a from t2 where t2.a = t1.a);
sql2: select * from t1 where t1.a not in (select a from t2 where t2.a is not null);
我认为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.