Jump to content

Multiple conditions in MySQL query (including date)


Go to solution Solved by requinix,

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!

  • Solution

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)
Edited by requinix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.