[英]CASE in WHERE expression using IN clause
I need that when var1 is equal to 'Y' the query show the result of the table with the column = 'Y', but when var1 = 'N' the result should be the data with column with 'Y' and 'N'.我需要当 var1 等于 'Y' 时,查询显示列 = 'Y' 的表的结果,但是当 var1 = 'N' 时,结果应该是带有 'Y' 和 'N' 列的数据. I need to put it in a where clause, cause I'm using oracle forms.
我需要将它放在 where 子句中,因为我使用的是 oracle 形式。 I tried this way but the query didn't show any result:
我试过这种方式,但查询没有显示任何结果:
SELECT *
FROM table
WHERE column1 IN ((CASE WHEN var1 = 'Y' THEN q'[('Y')]'
ELSE TO_CHAR(q'[('Y','N')]')
END))
Can you help me?你能帮助我吗? Thank you.
谢谢你。
There is no need for CASE
logic here, as you can fit this into a regular WHERE
clause with boolean logic, wrapping each condition (var1 = 'Y', var1 <> 'Y')
in a ()
group.这里不需要
CASE
逻辑,因为您可以将其放入带有布尔逻辑的常规WHERE
子句中,将每个条件(var1 = 'Y', var1 <> 'Y')
包装在()
组中。
SELECT *
FROM table
WHERE
(var1 = 'Y' AND column1 = 'Y')
OR (var1 <> 'Y' AND column1 IN ('Y','N'))
Note, I used var1 <> 'Y'
here to emulate your ELSE
case, but if it is only two possible values Y/N
you may use var1 = 'N'
for clarity.请注意,我在这里使用
var1 <> 'Y'
来模拟您的ELSE
情况,但如果它只有两个可能的值Y/N
,为了清楚起见,您可以使用var1 = 'N'
。
WHERE
(var1 = 'Y' AND column1 = 'Y')
OR (var1 = 'N' AND column1 IN ('Y','N'))
Actually, if Y/N
are the only possible values for column1
, then it could be simplified to:实际上,如果
Y/N
是column1
唯一可能的值,那么它可以简化为:
WHERE
(var1 = 'Y' AND column1 = 'Y')
-- Returns all rows for column1 if Y,N are the only possible values
-- No need to explicitly filter it
OR (var1 <> 'Y')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.