将条件写为where子句的值

我有一个名为_gender的变量,即为0-1或2

0 male
1 female
2 everyone

我想实现变量为所有人的情况,然后我希望性别为0或1

但是如果它是0或1,那么我应该与仅作为变量的用户匹配

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)"
评论
寂寞在掉泪
寂寞在掉泪

我认为您正在寻找类似的东西:

where country = 1 and
      (gender = _gender or _gender = 2)

Often null is used for the value to mean everyone. In that case, you need to phrase it as:

where country = 1 and
      (gender = _gender or _gender is null)
点赞
评论