首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

用in和not in统计的数目为什么不是总和,该怎么解决

2012-01-22 
用in和not in统计的数目为什么不是总和大家帮我分析分析这三条语句用in和notin统计的数目为什么不是总和SQ

用in和not in统计的数目为什么不是总和
大家帮我分析分析这三条语句   用in和not   in统计的数目为什么不是总和
SQL>   select   count(*)   from   rkb   where   fl= '1 '   and   jldw   in   (select   jldw   from   rkb   where   fl= '0 ');

    COUNT(*)
----------
                92

SQL>   select   count(*)   from   rkb   where   fl= '1 '   and   jldw   not   in   (select   jldw   from   rkb   where   fl= '0 ');

    COUNT(*)
----------
                  0

SQL>   select   count(*)   from   rkb   where   fl= '1 ';

    COUNT(*)
----------
              201


[解决办法]
NULL不包含在IN与NOT IN中
select jldw from rkb where fl= '0 '肯定有201-0-91=110个NULL
[解决办法]
select count(*) from rkb where fl= '1 ' and jldw not in (select jldw from rkb where fl= '0 ');

COUNT(*)
----------
0
是这句话出了问题,not in 如果里面有空值的话,全部返回null,所以你这得到0,
select count(*) from rkb where fl= '1 ' and jldw not in (select jldw from rkb where fl= '0 ' and jldw is not null);

试试

热点排行