Skip to content
Advertisement

Write Condition as a value for the where clause

I have a variable called _gender thats going to be either 0, 1 or 2

0 male
1 female
2 everyone

I want to achieve the case where _gender is everyone, then I want the gender to be 0 or 1

But if it was 0 or 1, then I should match with users that are only as the variable

SELECT * FROM TABLE
WHERE COUNTRY = 1
AND Gender = --"IF _gender == 2, then I need to get gender either = 0 or 1, else I get the value of the variable (to match 0 or 1)"

Advertisement

Answer

You have to Denote Variable starting with @.

Lets consider variable @gender

If you want when @gender is everyone, then the gender to be 0 or 1.

select * from TABLE where Country=1 
and  Gender = (case when @gender=2 then 0 else @gender end)

If you want when @gender is everyone, then the all type of gender from your table.

select * from TABLE where Country=1 
and  Gender = (case when @gender=2 then Gender else @gender end)

For reference click below link Code Reference Link Click Here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement