4

我必须使用像下面这样的条件 where 子句,这个子句的工作版本是什么?

AND CASE WHEN b.kurum_turu = 1 
         THEN u.kod  in ('1125', '2975', '1127', '4460', '1128', '1126')  
    ELSE u.kod  in ('1125', '2975')                 
END
4

5 回答 5

10

听起来你想要一个WHERE子句:

WHERE
(
  b.kurum_turu = 1
  AND u.kod  in ('1125', '2975', '1127', '4460', '1128', '1126')
)
OR
(
  u.kod  in ('1125', '2975') 
)
于 2012-12-19T11:51:14.153 回答
2

您可以将其重写为

...
WHERE
    (
        (b.kurum_turu = 1 AND u.kod in ('1125', '2975', '1127', '4460', '1128', '1126'))
        OR
        ((b.kurum_turu IS NULL OR b.kurum_turu != 1) AND u.kod in ('1125', '2975'))
    )
于 2012-12-19T11:54:53.617 回答
2

我想这就是你需要的..

 where ...AND CASE WHEN b.kurum_turu = 1 
         and u.kod  in ('1125', '2975', '1127', '4460', '1128', '1126') then 1  
         when  u.kod  in ('1125', '2975') then 1
     else 0      
END=1
于 2012-12-19T11:57:37.967 回答
0

我的方法是这样的:

 AND 1 = CASE
         WHEN  b.kurum_turu = 1 AND u.kod  in ('1125', '2975', '1127', '4460', '1128', '1126') THEN 1 ELSE 1 END
于 2017-07-05T13:48:10.063 回答
0

通过使用转换字符串列表CONNECT BY以获得条件IN语句。

如果您有多个条件,则此解决方案很有用,因为您可以将IN子句编写为单个表达式,而无需任何其他 AND、OR 限制。

WHERE u.kod IN 
(
  SELECT rowdata FROM
  ( WITH DATA AS 
    (SELECT CASE WHEN b.kurum_turu = 1 THEN '1125, 2975, 1127, 4460, 1128, 1126' ELSE '1125, 2975' END rowdata FROM dual )
    SELECT trim(regexp_substr(rowdata, '[^,]+', 1, LEVEL)) rowdata FROM DATA CONNECT BY instr(rowdata, ',', 1, LEVEL - 1) > 0
  )
)

这样做的缺点是逗号分隔值到数据行的转换有点复杂......但是您可以将这个复杂的部分提取到一个命名良好的函数中以隐藏这种复杂性。

于 2018-11-21T17:57:32.243 回答