T-Sql

如何在 CASE THEN 語句中使用 OR 運算符

  • April 29, 2019

我有這樣的情況

select col1, col2
from table1
where col1 = case when (booleanexp1) then a OR col1 = b 
                 when (booleanexp1) then c OR col1 = d end

where col1 = a or col1 = b我的意思是當 booleanexp1 為 TRUE 時我需要得到,但我得到一個語法錯誤?執行此類操作的正確方法是什麼?

假設兩個booleanexp1s 是印刷錯誤:

select col1, col2
from table1
where ((booleanexp1) AND (col1 IN (a, b)))
  or ((booleanexp2) AND (col1 IN (c, d)))

PS。not (booleanexp1)(booleanexp2)…的一種可能選擇

select col1, col2
from table1
where col1 = case 
              when (booleanexp1)  OR col1 = b  then a
              when (booleanexp1)  OR col1 = d  then c end

這部分也是布爾表達式

引用自:https://dba.stackexchange.com/questions/234633