Jump to content

Multiple conditions in MySQL query (including date)


happypete

Recommended Posts

Hi,

 

I'm trying the following query but it seems to ignore the part about the date....

"SELECT * FROM data WHERE incident = '1' OR incident = '2' OR incident = '3'  AND (DATE(date) BETWEEN 2013-08-19 AND 2013-10-04)" 

I've tried different formats:

"SELECT * FROM data WHERE incident = '1' OR incident = '2' OR incident = '3'  AND (date >= '2013-08-19' AND date <= '2013-10-04')"

some guidance in the right direction would be great!

AND has higher precedence than OR. That means you actually wrote something more like

SELECT * FROM data WHERE (incident = '1') OR (incident = '2') OR (incident = '3'  AND (DATE(date) BETWEEN 2013-08-19 AND 2013-10-04))
Which isn't what you want.

 

Use parentheses to change the outcome, or use IN and reduce the expression to a simpler X AND Y.

SELECT * FROM data WHERE incident IN ('1', '2', '3') AND (DATE(date) BETWEEN 2013-08-19 AND 2013-10-04)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.