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

时间:2010-05-10 17:29:26

标签: sql mysql

我正在尝试编写一个从三个不同事件返回相同结果的查询,但我认为我做错了。我可以针对一个事件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"

4 个答案:

答案 0 :(得分:28)

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

答案 1 :(得分:2)

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")

当你混合AND和OR时,使用parens将事物组合在一起是有帮助的。即使逻辑没有必要,有时也可以帮助其他人理解你的意图。

答案 2 :(得分:1)

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")

答案 3 :(得分:0)

您需要在括号中包含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")