Question

I'm not super awesome at SQL. I'm filtering a concrete5 collection with the following and not sure how to get all of it working. How do I use the parentheses correctly to separate the AND and OR statements?

This doesn't do anything:

$this->filter(null, "(
                (DATE_SUB(expirationDate, INTERVAL 30 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
                OR
                (DATE_SUB(expirationDate, INTERVAL 5 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
                OR
                (DATE_SUB(expirationDate, INTERVAL 1 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
            )
            AND
            (DATE_ADD(warnedDate, INTERVAL 4 DAY) <= NOW())");

This one mostly works (removed parentheses around the statements before the AND) but doesn't seem to filter the clause after AND... the "DATE_ADD(warnedDate, INTERVAL 4 DAY) <= NOW())":

$this->filter(null, "
                (DATE_SUB(expirationDate, INTERVAL 30 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
                OR
                (DATE_SUB(expirationDate, INTERVAL 5 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
                OR
                (DATE_SUB(expirationDate, INTERVAL 1 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())

            AND
            (DATE_ADD(warnedDate, INTERVAL 4 DAY) <= NOW())");
Was it helpful?

Solution

As mentioned in the comments the two clauses for 5 day and 1 day are redundant, because a 30 day interval to today already includes 5,1 day to today so remove them. Try something like this

(DATE_SUB(expirationDate, INTERVAL 30 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
AND
(DATE_ADD(warnedDate, INTERVAL 4 DAY) <= NOW())

NOTE: AND is used when you want BOTH of the conditions to be true and OR for at least one condition to be true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top