---
title: "oracle的or操作符使用错误"
author: "paynexss"
date: "2017-06-09T10:38:16Z"
tags: ["Oracle"]
categories: ["数据库"]
slug: "oracle-or"
draft: false
lastmod: "2017-06-09T10:38:16Z"
id: 2378684
url: "https://paynexss.iteye.com/blog/2378684"
---

工作中，发现一处数据计算结果异常，检查原始数据，没有发现问题，再手工计算，和系统计算结果一比，吓一跳：竟然大出了很多倍，于是，开始检查程序，发现有如下调用：  
　　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的应用。