oracle 的 or 操作符引起的大問題

來源:互聯網
上載者:User

        工作中,發現一處資料計算結果異常,檢查未經處理資料,沒有發現問題,再手工計算,和系統計算結果一比,嚇一跳:竟然大出了很多倍,於是,開始檢查程式,發現有如下調用:

 

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的應用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.