SQL 查询选择 where A='foo'其中 (A=“bar” 和 B=“baz”)
我有下一个 SQL 查询:
SELECT FIRST_NAME, LAST_NAME
FROM MYTABLE
WHERE STATUS = 1 AND VARIABLE IN ( 'PR', 'AR' ) AND SPECIAL = 1;
SPECIAL 是一个附加条件,如果我之前在表单中选择了“PR”(复选框),则会添加该条件。问题是,通过这个查询,我不再获得 VARIABLE 为“AR”的行。
现在,这个想法是,当 VARIABLE 为“PR”或“AR”时,此查询应返回任何行,但如果它是“PR”,则来自此仅“PR”的组,它应仅返回 特殊。我的意思是,它仍然应该显示 VARIABLE = 'AR' 的行,但如果 SPECIAL = 1,那么“PR”的行应该只是那些 SPECIAL = 1 的行。
I've got the next SQL query:
SELECT FIRST_NAME, LAST_NAME
FROM MYTABLE
WHERE STATUS = 1 AND VARIABLE IN ( 'PR', 'AR' ) AND SPECIAL = 1;
SPECIAL is an additional condition which is added if I previously selected 'PR' (checkbox) in my form. The thing is, with this query, I'm no longer getting rows where VARIABLE is 'AR'.
Now, the idea is that this query should return any rows when VARIABLE is 'PR' or 'AR', but if it is 'PR', from this 'PR'-only group, it should return only the SPECIAL ones. I mean, it should still display rows with VARIABLE = 'AR', but if SPECIAL = 1, then the ones that are 'PR', should be only those where SPECIAL = 1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于这是一项作业,我认为最好解释一下如何做,而不是仅仅给您复制和粘贴的代码。
您想要选择 STATUS = 1 的行且以下之一:
或:
使用
AND
和OR
的组合。请记住,AND
的优先级高于OR
。最好使用括号来明确如何计算表达式。Since it is an assignment I think it is better to explain how to do it than to just to give you the code to copy and paste.
You want to select a row where STATUS = 1 and one of:
or:
Use a combination of
AND
andOR
. Remember thatAND
has higher precedence thanOR
. It is a good idea to use parentheses to be explicit about how the expression should be calculated.SQL 对逻辑运算符有非常强大的支持,您不仅限于 AND。
SQL has very powerful support for logical operators, you aren't limited to just AND.
如果我理解正确并且您想要 ("AR") 或 ("PR" 和 SPECIAL = "1"),那么请尝试以下操作:
If I'm understanding right and you want either ("AR") or ("PR" and SPECIAL = "1"), then try this:
你需要 and OR 语句,试试这个:
You need and OR statement, try this:
我认为要使其按照您想要的方式工作,您需要做的就是将不同的标准分解为用括号括起来的单独的逻辑分组。如果我正确理解你的问题,你想要这样的东西:
I think all you need to do to get this to work the way you desire is to break up the different criteria into separate logical groupings surrounded by parenthesis. If I understand your question correctly, you want something like this: