簡體   English   中英

在嵌套和條件上應用OR條件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM