[英]Apply OR condition on nested and condition
我试图了解[或]条件如何在嵌套和条件上工作。
以下是我的示例查询
select * from t1 as a join t2 as b on a.id=b.id
where (a.value > 110 and a.value <= 120)
OR
(a.value > 120 and b.age > 20 and b.status='A')
上面的陈述是否将OR条件应用于完整的第一个(a.value> 110和a.value <= 120)和第二个(a.value> 120和b.age> 20并且b.status ='A')或将它将OR条件应用于a.value <= 120和a.value> 120
有人可以帮忙了解一下,因为我是sql的新手,所以有点混乱???
感谢您的时间和帮助:-)
提前致谢
查询将返回内部任意复杂条件s true的数据
let say c1= (a.value > 110 and a.value <= 120)
Or
c2=(a.value > 120 and b.age > 20 and b.status='A')
因此,如果c1或c2为true,则查询将被保留数据,但如果c1和c1均为false,则查询将不会返回数据
如果使用括号,答案是明确的,因为首先要评估括号的内容,而不是它们之间的OR
。
就像在Oracle条件优先级中一样AND
在 OR
之前先对AND
进行排序,您甚至可以删除括号以得到相同的效果-请参见下面的示例(简化为重点)。
with cnt as (
select rownum id from dual connect by level <= 10)
select * from cnt where
(id > 3 and id <= 5) or (id >= 7 and id < 10);
ID
----------
4
5
7
8
9
with cnt as (
select rownum id from dual connect by level <= 10)
select * from cnt where
id > 3 and id <= 5 or id >= 7 and id < 10;
ID
----------
4
5
7
8
9
如果要对AND
进行OR
优先排序,则需要括号,例如下面的示例
(id < 3 or id < 7) and id > 1
如果删除括号,您将从以下谓词中获得结果:
id < 3 or (id < 7 and id > 1)
高温超导
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.