Oracle - Where子句中的Case

时间:2014-01-22 13:09:04

标签: sql oracle case where

我正在尝试在Oracle数据库中执行查询。该查询在where子句中有case构造。

where
    sale.op = 2 and
        case when (:stat = -11) then (sale.type_id = 27 or sale.type_id = 28 or sale.type_id = 29)
    else
    (sale.type_id = 27)
    end

但是我收到以下错误:

  

ORA-00907:缺少右括号。

在Derby SQL中,这有效。有人可以帮帮我吗? 谢谢。

4 个答案:

答案 0 :(得分:5)

where sale.op = 2 
and (     (:stat = -11 and sale.type_id in (27, 28, 29))
      or  (:stat <> -11 and sale.type_id = 27)
    )

答案 1 :(得分:1)

尝试此查询:

where
    sale.op = 2 and
        ((:stat = -11 and (sale.type_id = 27 or sale.type_id = 28 or sale.type_id = 29)) 
       or (:stat <> -11 and sale.type_id = 27))

答案 2 :(得分:1)

您也可以尝试不使用CASE,使用简单的运算符AND和OR:

where
    sale.op = 2 and ((:stat = -11 and (sale.type_id = 27 or sale.type_id = 28 or sale.type_id = 29))  
        OR (:stat <> -11 and sale.type_id = 27))

答案 3 :(得分:1)

尝试此查询

where 
     sale.op =2 and
     ((:stat = -11 and sale.type_id=any(27,28,29)) or
       (:stat <> -11 and sale.type_id = 27))

看起来更清楚!!!