I have a query for selecting orders and I want to select between two dates. But I also want to select all orders that are of one of 3 or 4 statuses. I have the following
SELECT *
WHERE created BETWEEN '1296360000' AND '1322884800'
AND order_status = 'pending'
OR order_status = 'processing'
OR order_status = 'payment_received'
OR order_status = 'completed'
But the problem is it seems to select everything with order status of one of those 4. And completely ignores the BETWEEN if I change the order of the BETWEEN to be after the order status OR‘s it still includes orders with a created timestamp after the '1322884800'
Where should I put the between or how can I do this to select orders between X and Y with a order status of one of 4 (or 3, or 2, or 1) value(s)?
Put the
order_statusclause in parentheses (or better yet, use an IN list).The way you have it now, the precedence is being interpreted as:
This would evaluate to true if any of the last three order status are found, regardless of the
createddate.