微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

“不存在”和“不存在”有什么区别?

如何解决“不存在”和“不存在”有什么区别?

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。