Using the OR condition after BETWEEN does not work mysql

I have SQL code that has a condition BETWEENand OR.

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 I use ANDinstead OR, the code works fine. But I need ORinsteadAND

How to resolve this error. Where am I mistaken?

+4
source share
1 answer

You messed up your parentheses:

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

Must be:

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

BETWEEN two arguments are needed and should not have parentheses around the two arguments.

Like this:

o.order_date BETWEEN X AND Y  

Not this way:

o.order_date BETWEEN (X AND Y)  
+4
source

Source: https://habr.com/ru/post/1676978/


All Articles