MySQL: question about SELECT WHERE AND / OR

I'm trying to write a query that returns the same result from three different events, but I think I'm doing it wrong. I can run my request with a single event id and it works. How can I choose all three? Here is what I still have:

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" 
+6
sql mysql
source share
4 answers
 SELECT * FROM `Registrations` WHERE `Role` = "Attendee" AND `RegistrationStatus_ID` = "1" AND `DigSignature` IS NULL AND `Event_ID` in ("147", "155", "160") 
+25
source share
 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 mix AND and OR, it is useful to use parens to group things together. Even when this is not necessary for logic, it is sometimes useful for others to understand your intentions.

+2
source share

AND and OR have the same priority.

 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") 
+1
source share

In parentheses you need to enclose the OR operators:

 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") 
0
source share

All Articles