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

标签 mysql

我需要做一个选择,比如,

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/

相关文章:

c# - SQL批量上传

MySQL 表 : BIT(1) not null default b' 1', however, always default to ' 0'

mysql - SQL 选择查询 where 列 LIKE

php - 使用 SQL 语句重命名 MySQL 表

php - 如何计算 mysql-rows 之间的差异,有点不同

php - 通过解析访问日志或HTTP参数来检测是否存在sql注入(inject)攻击。 PHP 网站

mysql - 忽略类型转换 IN

php - 如何在 Laravel 中建立表之间的关系?

mysql - 为领域做资本值(value)

c# - 使用MySqlCommand参数时如何查看命令字符串?