如果`column`!= -1,则从表WHERE`column` = x中选择*

时间:2015-06-29 09:28:22

标签: mysql

我需要做一个选择,

Select * from table WHERE column = x if column != -1

但我现在不知道。

任何人都知道或做过类似的事情吗?

感谢。

1 个答案:

答案 0 :(得分:4)

你也应该这样写,

Select * from table 
WHERE 
1 = case when column != -1 then 
        case when column = x then 1 else 0 end
    else 1 end

你可以在where子句中使用case。 同样,您可以添加更多条件标准,例如

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

这只是一个如何将更多条件标准集成在一起的示例,即使您可以在其他部分中添加更多案例。

相关问题