Jithin Varghese Jithin Varghese - 6 months ago 24
MySQL Question

using OR condition after BETWEEN not working mysql

I have an SQL code that has

BETWEEN
and
OR
condition.

SELECT * FROM tbl_order o INNER JOIN tbl_contacts c ON c.contacts_id = o.contacts_id LEFT JOIN tbl_title t ON t.title_id = c.title_id LEFT JOIN tbl_assign a ON (a.order_id = o.order_id AND a.order_no_first = o.order_no_first) WHERE o.order_status = 1 AND o.order_date BETWEEN DATE(DATE_ADD(o.order_date, INTERVAL -2 DAY) AND CURDATE()) OR o.order_print = 1 GROUP BY o.order_id


The above code throws an error,

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR o.order_print = 1 GROUP BY o.order_id LIMIT 0, 25' at line 1


If am using
AND
instead of
OR
, the code works perfectly. But I need
OR
instead of
AND


How to resolve this error. Where am doing wrong?

Answer Source

You mixed up your parentheses:

o.order_date BETWEEN DATE(DATE_ADD(o.order_date, INTERVAL -2 DAY) AND CURDATE())

Should be:

o.order_date BETWEEN DATE(DATE_ADD(o.order_date, INTERVAL -2 DAY)) AND CURDATE()

BETWEEN needs two arguments, and must not have parentheses around the two arguments.

Like this:

o.order_date BETWEEN X AND Y  

Not like this:

o.order_date BETWEEN (X AND Y)