简体   繁体   English

WHERE子句中的MySQL条件IF语句

[英]MySQL Conditional IF statement in WHERE clause

I'm going nuts...cannot figure this out after a gazillion google searches. 我疯了......在搜索了无数的Google之后,无法解决这个问题。 I have the following table (Net) 我有下表(净额)

I'm trying to select the rows that matches owner/privacy info. 我正在尝试选择与所有者/隐私信息匹配的行。

ID       | net       | owner        |   privacy
+++++++++++++++++++++++++++++++++++++++++++++++
1        | 1234      | bob          |   1
2        | 2345      | charles      |   1
3        | 3456      | city         |   0
4        | 4567      | public       |   0

If Bob is extracting rows it should go like this: IF (owner = "bob") then select the row if privacy = 0 OR 1 ELSE select row if privacy = 0 如果Bob正在提取行,则应该这样:IF(owner =“ bob”),然后如果privacy = 0则选择该行,否则选择1,如果privacy = 0则选择该行。

He can extract his private row (privacy = 1) and any other row where privacy = 0 他可以提取自己的私有行(privacy = 1)以及任何其他privacy = 0的行

If've tried this but doesn't give any result: 如果尝试过此操作但未产生任何结果:

SELECT * FROM `Net` WHERE IF (STRCMP(owner, 'bob'), 'privacy < 2', 'privacy = 0')

You could use the or boolean operator to simplify this logic: 您可以使用or布尔运算符简化此逻辑:

SELECT *
FROM   my_table
WHERE  (owner = 'bob' AND (privacy IN (0, 1)) OR privacy = 0

Assuming that privacy can ever only be 0 or 1, if the owner is bob , you could just omit the privacy and branch altogether: 假设privacy只能是0或1,如果所有者是bob ,则可以完全忽略隐私权and分支:

SELECT *
FROM   my_table
WHERE  owner = 'bob' OR privacy = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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