"不在"和"不存在"之间有什么区别?

Gol*_*old 13 sql database oracle

Oracle查询之间not in和之间的区别是什么not exists

not in什么时候使用?而且not exist

Nic*_*int 24

如果结果中包含值,则NOT INNOT EXISTS之间的区别变得清晰NULL.

例如:

create table test_a (col1 varchar2(30 char));
create table test_b (col1 varchar2(30 char));

insert into test_a (col1) values ('a');
insert into test_a (col1) values ('b');
insert into test_a (col1) values ('c');
insert into test_a (col1) values ('d');
insert into test_a (col1) values ('e');

insert into test_b (col1) values ('a');
insert into test_b (col1) values ('b');
insert into test_b (col1) values ('c');
insert into test_b (col1) values (null);
Run Code Online (Sandbox Code Playgroud)

注意:它们的关键区别在于test_b包含一个null值.

select * from test_a where col1 not in (select col1 from test_b);
Run Code Online (Sandbox Code Playgroud)

没有返回任何行

select * from test_a where 
    not exists
        (select 1 from test_b where test_b.col1 = test_a.col1);
Run Code Online (Sandbox Code Playgroud)

返回

col1
====
d
e
Run Code Online (Sandbox Code Playgroud)


sha*_*esh 7

我认为它有同样的目的.

not in可以采用文字值,而not exists需要查询来比较结果.

编辑:not exists可以很好用,因为join如果条件使用索引的列,它可以使用外部查询并可以导致使用索引.

EDIT2:也看到这个问题.

编辑3:让我回过头来看看.
看到这个链接.我认为,这一切都取决于数据库如何转换这个和数据库/索引等.