MySQL:关于 SELECT WHERE AND/OR 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2804881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:01:31  来源:igfitidea点击:

MySQL: Question about SELECT WHERE AND/OR

sqlmysql

提问by Andrew

I'm trying to write a query that returns the same result from three different events, but I think I am doing it wrong. I can run my query against one event ID and it works. How can I select all three? Here's what I have so far:

我正在尝试编写一个从三个不同事件返回相同结果的查询,但我认为我做错了。我可以针对一个事件 ID 运行我的查询并且它可以工作。我怎样才能选择所有三个?这是我到目前为止所拥有的:

SELECT * FROM `Registrations` 
WHERE `Role` = "Attendee" AND `RegistrationStatus_ID` = "1" AND `DigSignature` IS NULL
    AND `Event_ID` = "147" OR `Event_ID` = "155" OR `Event_ID` = "160"

回答by RedFilter

SELECT * 
FROM `Registrations`  
WHERE `Role` = "Attendee" 
    AND `RegistrationStatus_ID` = "1" 
    AND `DigSignature` IS NULL 
    AND `Event_ID` in ("147", "155", "160")

回答by Paul Tomblin

SELECT * FROM `Registrations` 
WHERE `Role` = "Attendee" AND `RegistrationStatus_ID` = "1" AND `DigSignature` IS NULL
    AND (`Event_ID` = "147" OR `Event_ID` = "155" OR `Event_ID` = "160")

When you're mixing AND and OR, it's helpful to use parens to group things together. Even when it's not necessary for logic, it's sometimes helpful for others to understand your intentions.

当您混合使用 AND 和 OR 时,使用括号将事物组合在一起会很有帮助。即使对于逻辑来说不是必需的,有时也有助于其他人理解您的意图。

回答by Ignacio Vazquez-Abrams

ANDand ORhave equal precedence.

ANDOR享有同等优先权。

SELECT * FROM `Registrations` 
WHERE `Role` = "Attendee" AND `RegistrationStatus_ID` = "1" AND `DigSignature` IS NULL
    AND (`Event_ID` = "147" OR `Event_ID` = "155" OR `Event_ID` = "160")

回答by IMHO

You need to wrap OR statements in parentheses:

您需要将 OR 语句括在括号中:

SELECT * FROM `Registrations` 
WHERE `Role` = "Attendee" AND `RegistrationStatus_ID` = "1" AND `DigSignature` IS NULL
    AND (`Event_ID` = "147" OR `Event_ID` = "155" OR `Event_ID` = "160")