簡體   English   中英

Oracle改進查詢where子句

[英]Oracle improve query where clause

我有一個小問題。 我想執行該查詢:

WHERE (column1 = 'tetxt1'  and column2 = 'tetxt2' and column3 = 'tetxt3 ) 
OR (column2 = 'tetxt2' and column3 = 'tetxt3' ) 
OR (column1 = 'tetxt1' and column3 = 'tetxt3' ) 
OR (column1 = 'tetxt1' and column2 = 'tetxt2' ) 
OR ( column3 = 'tetxt3' ) 
OR ( column1 = 'tetxt1 ) 
OR ( column2 = 'tetxt2 )

換句話說,where子句列之一可以為空,但是我需要使用and查詢具有值的列。 因此,如果我只有column1和column2的值,但沒有column3的值,那么將使用and條件查詢這兩個值。 我認為那不會起作用:

Where (column1 = 'tetxt1'  or column2 = 'tetxt2' or column3 = 'tetxt3')

我給出的第一種方法不是最好的。 您有任何想法如何改善查詢? 非常感謝!

您不會通過以下方式獲得相同的效果:

WHERE 
column1 IN('tetxt1','tetxt2','tetxt3')
or column2 IN('tetxt1','tetxt2','tetxt3')
or column3 IN('tetxt1','tetxt2','tetxt3')

假設“空”表示NULL ,則可以使用COASLESCE關鍵字。

WHERE (COALESCE(column1, 'text1') = 'tetxt1'  and COALESCE(column2, 'text2')  = 'tetxt2' and COALESCE(column3, 'text3')  = 'tetxt3 ) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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