oracle not in 问题

来源:互联网 发布:mac rebel什么质地 编辑:程序博客网 时间:2024/05/17 23:30

写了个sql 语句 ,大概逻辑是这样: 查询 在8月份出现,在九月份没有出现 的 用户 。
很简单的语句,如下

select distinct e.assign_oper          from eir_print_bill e         where e.assign_time between               to_date('2017-09-01 00:00:00', 'yyyy-MM-dd hh24:mi:ss') and               to_date('2017-09-30 23:59:59', 'yyyy-MM-dd hh24:mi:ss')           and e.assign_oper not in               (select distinct epb.assign_oper                  from eir_print_bill epb                 where epb.assign_time between                       to_date('2017-08-01 00:00:00', 'yyyy-MM-dd hh24:mi:ss') and                       to_date('2017-08-31 23:59:59', 'yyyy-MM-dd hh24:mi:ss'));

查询的结果,空的 。
好吧,于是执行 前半部分,也就是八月份的 ,6条 数据 ; 后部分,也就是九月份的 ,4条数据 。

再理了一遍逻辑,完全没发现有什么问题 。陷入了困惑 ……

后来注意到: 后部分,也就是九月份的数据中有个值是 空的 ,于是试一下,去空值 。修改后语句如下:

select distinct e.assign_oper  from eir_print_bill e where e.assign_time between       to_date('2017-09-01 00:00:00', 'yyyy-MM-dd hh24:mi:ss') and       to_date('2017-09-30 23:59:59', 'yyyy-MM-dd hh24:mi:ss')   and e.assign_oper is not null   and e.assign_oper not in       (select distinct epb.assign_oper          from eir_print_bill epb         where epb.assign_time between               to_date('2017-08-01 00:00:00', 'yyyy-MM-dd hh24:mi:ss') and               to_date('2017-08-31 23:59:59', 'yyyy-MM-dd hh24:mi:ss')           and epb.assign_oper is not null);

好了,正确的结果出来了 。

结论 :

not in 里面的值不能有 空值 。