我需要做一个选择,比如,
Select * from table WHERE column = x if column != -1
但我现在还不知道。
有人知道或过去做过类似的东西吗?
谢谢。
最佳答案
你也应该这样写,
Select * from table
WHERE
1 = case when column != -1 then
case when column = x then 1 else 0 end
else 1 end
您可以在 where 子句中使用 case when。 同样,您可以添加更多条件条件,例如,
Select * from table
WHERE
1 = case when column != -1 then
case when column = x then 1 else 0 end
else 1 end
AND
1 = case when column1 [conditional operator] value then
case when column1 = xx then 1 else 0 end
else 1 end
这只是一个示例,您可以如何将更多条件标准集成在一起,即使您甚至可以在 else 部分添加更多情况。
关于mysql - Select * from table WHERE `column` = x if `column` != -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31112189/