[英]Using Case Statement in Where Clause (with column)
I want to use a case statement in a where clause. 我想在where子句中使用case语句。 But the where clause change depends on my equation. 但where子句的改变取决于我的等式。
For example: 例如:
Table 表
ID Column1 Column2
1 2 3
2 4 1
3 5 4
4 4 7
Query 询问
select * from table
where (case when column1>=column2 then column2>3 else column1>3 end)
Expected output 预期产出
ID Column1 Column2
3 5 4
4 4 7
It sounds like you want your CASE
expression to be: 听起来你想要你的CASE
表达式是:
SELECT *
FROM dbo.YourTable
WHERE CASE
WHEN column1 >= column2 AND column1 > 4 THEN 1
WHEN column1 < column2 AND column2 > 4 THEN 1
ELSE 0
END = 1
You don't need CASE
, you can use OR
: 你不需要CASE
,你可以使用OR
:
select * from table
where (column1 >= column2 and column1 > 3)
or (column1 < column2 and column2 > 3)
How about something like 怎么样的
select *
from table
where (
case
when column1>=column2
then column1
else column2
end > 4
)
If else
can be simulated using Or
& And
operator in where
clause.Try this. If else
可以使用where
子句中的Or
& And
运算符进行模拟。试试这个。
SELECT *
FROM tablename
WHERE ( column1 >= column2
AND column1 > 4 )
OR ( column1 < column2
AND column2 > 4 )
Try this: 试试这个:
SELECT *
FROM tableA A
WHERE (CASE WHEN A.column1 >= A.column2 THEN A.column2
ELSE A.column1
END) > 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.