在 where 子句中使用 case when 语句时如何使用 in 子句?
How do I use in clause while using case when statement in where clause?
如何在 case when 语句的 else 块中使用 in 子句,在 where 子句中,例如:
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);
另一种方法可以是:
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更新
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))
;
我想你可能需要修改以上备选方案一点点
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 );
如何在 case when 语句的 else 块中使用 in 子句,在 where 子句中,例如:
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);
另一种方法可以是:
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更新
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))
;
我想你可能需要修改以上备选方案一点点
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 );