oracle 的 or 操作符引起的大问题

来源:互联网 发布:佳仁按摩椅怎么样知乎 编辑:程序博客网 时间:2024/06/05 02:18

        工作中,发现一处数据计算结果异常,检查原始数据,没有发现问题,再手工计算,和系统计算结果一比,吓一跳:竟然大出了很多倍,于是,开始检查程序,发现有如下调用:

 

select tt.BC1610
  from TJ_V_PFI_BC1610_BC1620 tt
 where to_char(tt.tj_date,
'yyyymm') = '200811'
   and tt.regionid =
999
    or tt.regionid in (select b.regionid
                         from tj_performance_region_sub b
                        where b.type =
0
                          and b.parentregionid =
999)

 

    取出执行发现,结果确实异常,检查分析后发现,竟然是  where 条件的问题,是在条件中用到了 or 操作符,由于粗心的原因,没有注意到它们的关系,引起结果的异常,改成如下所示的SQL,测试正常:

 

select nvl(sum(tt.BC1610), 0)
  from TJ_V_PFI_BC1610_BC1620 tt
 where to_char(tt.tj_date,
'yyyymm') = '200811'
   and (tt.regionid =
999
    or tt.regionid in (select b.regionid
                         from tj_performance_region_sub b
                        where b.type =
0
                          and b.parentregionid =
999));

 

    唯一的区别是,第一句没有将两个 OR 条件括起来。

 

    工作中,用到 OR 的地方特别多,如果OR和其它的条件是并列的话,一定要注意OR的应用。

原创粉丝点击