tags:

views:

75

answers:

2

I tried this and it returned incorrect rows

SELECT `product_id`, `expiry` FROM `products_featured` 
WHERE `notified_expiry` = FALSE 
     AND `expiry` != 
    AND DATE_ADD(NOW(), INTERVAL 5 DAY) >= `expiry`

I want to select all rows where the expiry date is within 5 days from now.

The rows returned were

    [0]=>   array(2) {
    ["product_id"]=>
    string(2) "26"
    ["expiry"]=>
    string(10) "2010-07-07" }

    [1]=>   array(2) {
    ["product_id"]=>
    string(2) "38"
    ["expiry"]=>
    string(10) "2010-07-24" }

    [2]=>   array(2) {
    ["product_id"]=>
    string(2) "39"
    ["expiry"]=>
    string(10) "2010-07-07" }

As you can see, only one of those rows is to occur within 5 days (assuming NOW() is returning 2010-07-22 which I have confirmed).

What am I doing wrong? Thanks.

+2  A: 
SELECT `product_id`, `expiry` FROM `products_featured` WHERE `notified_expiry` = FALSE AND `expiry` != 0 AND DATE_ADD(NOW(), INTERVAL 5 DAY) >= `expiry` AND NOW() <= `expiry`
Sadat
Thank you! Seemed to work on my test data.
alex
It does not explain why your statement did not work. It's probably just selecting the rows with the part `NOW() <= expiry`. So an `expiry` of `2010-10-05` would pass although it shouldn't.
pascal
+1  A: 

SELECT product_id, expiry FROM products_featured WHERE notified_expiry = FALSE AND expiry != 0 AND DATEDIFF(NOW(), expiry) BETWEEN 0 AND 5

Trinity