我必须使用像下面这样的条件 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
我必须使用像下面这样的条件 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
听起来你想要一个WHERE
子句:
WHERE
(
b.kurum_turu = 1
AND u.kod in ('1125', '2975', '1127', '4460', '1128', '1126')
)
OR
(
u.kod in ('1125', '2975')
)
您可以将其重写为
...
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'))
)
我想这就是你需要的..
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
我的方法是这样的:
AND 1 = CASE
WHEN b.kurum_turu = 1 AND u.kod in ('1125', '2975', '1127', '4460', '1128', '1126') THEN 1 ELSE 1 END
通过使用转换字符串列表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
)
)
这样做的缺点是逗号分隔值到数据行的转换有点复杂......但是您可以将这个复杂的部分提取到一个命名良好的函数中以隐藏这种复杂性。