轉自:http://www.maomao365.com/?p=6873 摘要: 下文通過案例分析in 關鍵字在值為null的應用舉例, 分析出not in關鍵字在null值產生的異常信息 如下所示: 通過以上測試,我們可以看出not in 查詢關鍵字如果子表中存在空值,則無法查詢出任何記錄,會導致異 ...
轉自:http://www.maomao365.com/?p=6873
摘要:
下文通過案例分析in 關鍵字在值為null的應用舉例,
分析出not in關鍵字在null值產生的異常信息
如下所示:
CREATE TABLE testA(keyId INT) CREATE TABLE testB(keyId INT) GO INSERT INTO testA(keyId) VALUES (1) INSERT INTO testA(keyId) VALUES (2) INSERT INTO testA(keyId) VALUES (3) GO INSERT INTO testB(keyId) VALUES (1) INSERT INTO testB(keyId) VALUES (2) INSERT INTO testB(keyId) VALUES (4) INSERT INTO testB(keyId) VALUES (NULL) GO SELECT * FROM testA WHERE keyId IN (SELECT keyId FROM testB) -----輸出------ /* keyId 1 2 */ SELECT * FROM testA WHERE keyId not IN (SELECT keyId FROM testB) ------無輸出記錄----- /* keyId */ INSERT INTO testA VALUES (NULL) ---在testA表中插入空值 SELECT * FROM testA WHERE EXISTS( SELECT null FROM testB WHERE testA.keyId=testB.keyId ) ----輸出---- /* keyId 1 2 */ SELECT * FROM testA WHERE not EXISTS( SELECT null FROM testB WHERE testA.keyId=testB.keyId ) /* keyId 3 NULL */ SELECT * FROM testA WHERE testA.keyId NOT in ( SELECT testB.keyId FROM testB WHERE testB.keyId IS NOT NULL ) drop table testA drop table testB
通過以上測試,我們可以看出not in 查詢關鍵字如果子表中存在空值,則無法查詢出任何記錄,會導致異常產生,
需使用not exists獲取相應的空值信息