您的位置:首页 > 数据库 > Oracle

Oracle not in子连接查询不到值的问题(not in 不能查询null数据)

2015-07-13 10:49 806 查看
前几天在项目中,做数据导入时,发现not in和in 查出来的条数不互补。ATABLE2明明中有些记录在ATABLE3中不存在,但是not in时查不出记录。

CREATE TABLE ATABLE2
( "MRID" VARCHAR2(20 BYTE)
)

CREATE TABLE ATABLE3"
( "MRID" VARCHAR2(20 BYTE)
)

查询语句如下

select count(*) from atable2 where mrid not in (select mrid from atable3),查询结果为0。

经过几番检查,发现的not in 和null的问题。atable3表中存在mrid为null的记录,此时not in 查询不到值。

修改方案如下:在子查询中增加非空过滤

select count(*) from atable2 where mrid not in (select mrid from atable3 where mrid is not null)

完毕!

ps:count统计函数对null值也不适用,需用count(*).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: