如何在where子句中的case语句的else块中使用in子句,例如:
select *
from dummy_table dt
where dt.some_column_1 in
(case when
dt.some_column_2 in (9,10) then dt.some_column_3
else (dt.some_column_4,dt.some_column_5)
end);
答案 0 :(得分:1)
IN子句只能采用(a)expression list或(b)返回一组行的select语句。您的查询在括号内给它一个CASE语句。
我可以想到几种方法来做到这一点;还有更多。
select *
from dummy_table dt
where dt.some_column_1 in
(select dt.some_column_3 from dual where dt.some_column_2 in (9,10)
union select dt.some_column_4 from dual where dt.some_column_2 not in (9,10)
union select dt.some_column_5 from dual where dt.some_column_2 not in (9,10));
select *
from dummy_table dt
where dt.some_column_1 in
(case when dt.some_column_2 in (9,10) then dt.some_column_3 else null end,
case when dt.some_column_2 not in (9,10) then dt.some_column_4 else null end,
case when dt.some_column_2 not in (9,10) then dt.some_column_5 else null end)
;
select *
from dummy_table dt
where (dt.some_column_2 in (9,10) and dt.some_column_1 = dt.some_column_3)
or (dt.some_column_2 not in (9,10) and dt.some_column_1 in (dt.some_column_4, dt.some_column_5))
;
答案 1 :(得分:0)
另一种方法可以是:
SELECT *
FROM dummy_table dt
WHERE ( dt.some_column_2 IN ( 9, 10 )
AND dt.some_column_1 = dt.some_column_3 )
OR ( dt.some_column_2 NOT IN ( 9, 10 )
AND dt.some_column_1 IN ( dt.some_column_4, dt.some_column_5 ) );
在第二部分使用NOT IN进行更新
答案 2 :(得分:0)
我认为你可能需要修改上面的替代品
SELECT *
FROM dummy_table dt
WHERE ( dt.some_column_2 IN ( 9, 10 )
AND dt.some_column_1 = dt.some_column_3 )
OR ( dt.some_column_2 NOTIN ( 9, 10 ) AND dt.some_column_1 IN ( dt.some_column_4, dt.some_column_5 );