[英]case statement in where clause with in values oracle sql
I am working on Oracle Apex application. 我正在研究Oracle Apex应用程序。 I have a simple select query which is working fine but now I want to do if/else or case statement in that query.
我有一个简单的选择查询,它工作正常,但现在我想在该查询中执行if / else或case语句。
so query is below 所以查询如下
select value d,
id r
from dhm_lov_data
where lov_id = 103
and nvl(isdeleted,0) = 0
and parentid_1 =:p21_typeid
order by d
I want to change this query to add a condition on :P21_TYPEID
value. 我想更改此查询以添加条件
:P21_TYPEID
值。 Eg if :P21_TYPEID
value is 1 then in where clause it should be PARENTID_1 in (10,11)
otherwise PARENTID_1 = :P21_TYPEID
. 例如
:P21_TYPEID
如果:P21_TYPEID
值为1,则在where子句中,它应该是PARENTID_1 in (10,11)
否则PARENTID_1 = :P21_TYPEID
。
I tried to put it in case statement but it didn't work. 我试着把它放在case语句中,但它没有用。 Below is query I am trying to make work using a case statement
下面是查询我试图使用case语句进行工作
SELECT VALUE d, id r FROM DHM_LOV_DATA WHERE LOV_ID= 103 and nvl(ISDELETED,0) = 0
and
case when :P21_TYPEID = 848 then PARENTID_1 in (287,288)
else PARENTID_1 = :P21_TYPEID end;
order by d
A case
expression returns a value, not a condition you could just add to your where
clause. case
表达式返回一个值,而不是您可以添加到where
子句的条件。 But you could emulate this behavior with some boolean operators: 但您可以使用一些布尔运算符来模拟此行为:
SELECT value d, id r
FROM dhm_lov_data
WHERE lov_id = 103 AND
NVL(isselected, 0) = 0 AND
(:P21_TYPEID = 848 AND parentid_1 IN (287,288) OR
:P21_TYPEID = parentid_1)
ORDER BY d
Something like this ugly example, perhaps? 也许这个丑陋的例子?
select value d, id r
from dhm_lov_data
where lov_id = 103
and nvl(isdeleted, 0) = 0
and parentid_1 in (select :p21_typeid from dual
where :p21_typeid <> 848
union
select val from (select 287 val from dual
union
select 288 val from dual
)
where :p21_typeid = 848
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.