您好,登錄后才能下訂單哦!
之前寫過一篇關(guān)于NULL對(duì)in和not in結(jié)果的影響:Oracle的where條件in/not in中包含NULL時(shí)的處理。今天來看看exists和not exists中NULL值對(duì)結(jié)果的影響。
網(wǎng)上經(jīng)常看到關(guān)于in和exixts、not in和not exists性能比對(duì)和互換的例子,但它們真的就可以簡單互換么?我們通過下面的實(shí)驗(yàn)來看一下。
實(shí)驗(yàn)環(huán)境:Oracle 11.2.0.4
1、創(chuàng)建表并插入測(cè)試數(shù)據(jù)
create table t1 (id number); create table t2 (id number); insert into t1 values(1); insert into t1 values(2); insert into t1 values(3); insert into t1 values(4); insert into t1 values(null); commit; insert into t2 values(3); insert into t2 values(4); insert into t2 values(5); insert into t2 values(6); commit; zx@ORA11G>select * from t1; ID ---------- 1 2 3 4 5 rows selected. zx@ORA11G>select * from t2; ID ---------- 3 4 5 6 4 rows selected.
第一種情況:exists/in的查詢中不包含NULL,外層查詢包含NULL
zx@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id); ID ---------- 3 4 2 rows selected. zx@ORA11G>select * from t1 where id in (select id from t2); ID ---------- 3 4 2 rows selected.
從上面的查詢結(jié)果看出exists和in都查到了id=2和3的兩條數(shù)據(jù)。
第二種情況:not exists/not in的查詢中不包含NULL,外層查詢包含NULL
zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id); ID ---------- 1 2 3 rows selected. zx@ORA11G>select * from t1 where id not in (select id from t2); ID ---------- 1 2 2 rows selected.
從上面的結(jié)果中可以看到兩個(gè)查詢都查到了id=1和2這兩條記錄,但not exists還查到了t1表中id為NULL的行。原因是表t1中id為NULL的行exists(3,4,5,6)為False,但前面加了個(gè)not則返回結(jié)果就為True了。
第三種情況:exists/in的子查詢中包含NULL,外層查詢包含NULL
zx@ORA11G>insert into t2 values(null); 1 row created. zx@ORA11G>commit; Commit complete. zx@ORA11G>select * from t1 where id in (select id from t2); ID ---------- 3 4 2 rows selected. zx@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id); ID ---------- 3 4 2 rows selected.
從上面的結(jié)果中可以看出exist和in的結(jié)果是一致的。
第四種情況:not exists和not in的查詢中包含NULL
zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id); ID ---------- 1 2 3 rows selected. zx@ORA11G>select * from t1 where id not in (select id from t2); no rows selected
從上面的查詢結(jié)果中可以看出兩個(gè)結(jié)果差異很大,not exists把id=1和2和為NULL的值都查出來了,而not in查出來的結(jié)果為空。no in結(jié)果為空的原因可以參考之前的文章,not exists的原因與第二種情況類似。
第五種情況:not in/not exists的子查詢中無NULL值,外層查詢也無NULL值
zx@ORA11G>delete from t1 where id is null; 1 row deleted. zx@ORA11G>delete from t2 where id is null; 1 row deleted. zx@ORA11G>commit; Commit complete. zx@ORA11G>select * from t1 where id not in (select id from t2); ID ---------- 1 2 2 rows selected. zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id); ID ---------- 1 2 2 rows selected.
第六種情況:in/exists的子查詢中無NULL值,外層查詢也無NULL值
zx@ORA11G>select * from t1 where id in (select id from t2); ID ---------- 3 4 2 rows selected. zx@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id); ID ---------- 3 4 2 rows selected.
第七種情況:in/exists的子查詢中有NULL值,外層查詢無NULL值
zx@ORA11G>insert into t2 values(null); 1 row created. zx@ORA11G>commit; Commit complete. zx@ORA11G>select * from t1 where id in (select id from t2); ID ---------- 3 4 2 rows selected. zx@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id); ID ---------- 3 4 2 rows selected.
第八種情況:not in/not exists的子查詢中有NULL值,外層查詢無NULL值
zx@ORA11G>select * from t1 where id not in (select id from t2); no rows selected zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id); ID ---------- 1 2 2 rows selected.
從上面的八種情況我們可以總結(jié)如下:
1、in和exists在有無NULL的情況下可以相互轉(zhuǎn)換。
2、not in和not exists在都沒有NULL值的情況下才可以相互轉(zhuǎn)換。
參考:https://mp.weixin.qq.com/s/rHKBFMQrrBf1TiUo6UmEmQ
http://docs.oracle.com/cd/E11882_01/server.112/e41084/conditions013.htm#SQLRF52169
http://docs.oracle.com/cd/E11882_01/server.112/e41084/conditions012.htm#SQLRF52167
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。