Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Question about SELECT WHERE AND/OR

Tags:

sql

mysql

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"
like image 619
Andrew Avatar asked May 10 '10 17:05

Andrew


3 Answers

SELECT * 
FROM `Registrations`  
WHERE `Role` = "Attendee" 
    AND `RegistrationStatus_ID` = "1" 
    AND `DigSignature` IS NULL 
    AND `Event_ID` in ("147", "155", "160")
like image 178
D'Arcy Rittich Avatar answered Nov 03 '22 00:11

D'Arcy Rittich


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.

like image 24
Paul Tomblin Avatar answered Nov 03 '22 01:11

Paul Tomblin


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")
like image 20
Ignacio Vazquez-Abrams Avatar answered Nov 03 '22 00:11

Ignacio Vazquez-Abrams