MySQL查询与条件语句? [英] MySQL query with conditional statement?
问题描述
我有一个MySQL查询,保留了两个表的连接.这是当前的查询结果:
I have a MySQL query left joining two tables. This is the current query result:
id | login | privacy-weight | requires
--------------------------------------------
0 | user | 1 | NULL
0 | user2 | 1 | NULL
0 | user3 | 1 | privacy-weight
查询本身并不重要,因为我只想向查询中添加一个WHERE
条件.
The query itself is not important, as I'd only like to add a WHERE
condition to the query as it is now.
我只需要提取(用我自己的话)的值:
I need to fetch only values which (in my own words):
IF (`requires` = 'privacy-weight'), then `privacy-weight` must equal = 0;
这意味着,我需要这种情况:
That means, I need this condition:
WHERE `privacy-weight` = 0
但只有在满足以下条件的情况下:
BUT only if this is true:
requires = 'privacy-weight'
可以做到吗?
编辑
显然,这太难理解了,因此,示例输出:
Obviously this is too difficult to understand, therefore, an example output:
privacy-weight | requires
-------------------------
0 | NULL
1 | NULL
0 | privacy-weight
1 | NULL
这些将被忽略(不提取):
These would be ignored (not fetched):
privacy-weight | requires
-------------------------
1 | privacy-weight
推荐答案
您可以使用以下简单测试:
You could use this simple test:
WHERE (requires = 'privacy-weight' AND privacy-weight = 0) OR requires <> 'privacy-weight' OR requires IS NULL
第一部分(requires = 'privacy-weight' AND privacy-weight = 0)
阻止输出:
privacy-weight | requires
-------------------------
1 | privacy-weight
但保持:
privacy-weight | requires
-------------------------
0 | privacy-weight
第二部分OR requires <> 'privacy-weight' OR requires IS NULL
将保留以下内容:
While the second part OR requires <> 'privacy-weight' OR requires IS NULL
will keep the following ones:
privacy-weight | requires
-------------------------
0 | NULL
1 | NULL
1 | NULL
这篇关于MySQL查询与条件语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!