您好,登錄后才能下訂單哦!
之前寫過一篇關于NULL對in和not in結果的影響:Oracle的where條件in/not in中包含NULL時的處理。今天來看看exists和not exists中NULL值對結果的影響。
網上經常看到關于in和exixts、not in和not exists性能比對和互換的例子,但它們真的就可以簡單互換么?我們通過下面的實驗來看一下。
實驗環境:Oracle 11.2.0.4
1、創建表并插入測試數據
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.
從上面的查詢結果看出exists和in都查到了id=2和3的兩條數據。
第二種情況: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.
從上面的結果中可以看到兩個查詢都查到了id=1和2這兩條記錄,但not exists還查到了t1表中id為NULL的行。原因是表t1中id為NULL的行exists(3,4,5,6)為False,但前面加了個not則返回結果就為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.
從上面的結果中可以看出exist和in的結果是一致的。
第四種情況: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
從上面的查詢結果中可以看出兩個結果差異很大,not exists把id=1和2和為NULL的值都查出來了,而not in查出來的結果為空。no in結果為空的原因可以參考之前的文章,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.
從上面的八種情況我們可以總結如下:
1、in和exists在有無NULL的情況下可以相互轉換。
2、not in和not exists在都沒有NULL值的情況下才可以相互轉換。
參考: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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。