简体   繁体   中英

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

I need to make a select like,

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

but i have no idea for now.

Anyone know or made in past something like that?

Thanks.

You should also write like this,

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

You can utilize case when in where clause. Similarly you can add more conditional criteria like,

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

This is just an example how you can integrate more conditional criteria together, even though you can add more case when in else part even.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM