原文:
摘要:
下文通过案例分析in 关键字在值为null的应用举例, 分析出not in关键字在null值产生的异常信息 如下所示:CREATE TABLE testA(keyId INT)CREATE TABLE testB(keyId INT)GOINSERT INTO testA(keyId) VALUES (1)INSERT INTO testA(keyId) VALUES (2)INSERT INTO testA(keyId) VALUES (3)GOINSERT 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)-----输出------/*keyId12*/ 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 )----输出----/*keyId12*/SELECT * FROM testA WHERE not EXISTS( SELECT null FROM testB WHERE testA.keyId=testB.keyId )/*keyId3NULL*/SELECT * FROM testA WHERE testA.keyId NOT in ( SELECT testB.keyId FROM testB WHERE testB.keyId IS NOT NULL )drop table testAdrop table testB
通过以上测试,我们可以看出not in 查询关键字如果子表中存在空值,则无法查询出任何记录,会导致异常产生,
需使用not exists获取相应的空值信息 posted on 2018-08-17 09:13 阅读( ...) 评论( ...)