tags:

views:

60

answers:

4

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:

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"
+7  A: 
SELECT * 
FROM `Registrations`  
WHERE `Role` = "Attendee" 
    AND `RegistrationStatus_ID` = "1" 
    AND `DigSignature` IS NULL 
    AND `Event_ID` in ("147", "155", "160")
RedFilter
I guess you win the "quick post" award this time.
Paul Tomblin
perfect. thank you.
Andrew
A: 

You need to wrap OR statements in parentheses:

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")
IMHO
+1  A: 
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.

Paul Tomblin
A: 

AND and OR have equal precedence.

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")
Ignacio Vazquez-Abrams