4
    SELECT * FROM events  WHERE `date` 
    BETWEEN NOW() AND DATE_ADD
    (NOW(), INTERVAL 10 DAY) order by date

I am using the above code to extract the events data of ten days of interval. Now I want to Extract out all the data before this 10 days of interval and after 10 days of interval. How can I do that in a perfect way?

Please help. Advance thanks for your time

  • What do you mean? 10-20 days ago? 10-20 days into the future? 20-30, etc? – Rick James Oct 2 '16 at 21:46
  • I want both. Once I have to show past data and then I have show future data – Allen Titan Oct 3 '16 at 5:22
2

It is probably best to issue two queries.

SELECT * FROM events
    WHERE `date` >= NOW() - INTERVAL 10 DAY
      AND `date`  < NOW();

SELECT * FROM events
    WHERE `date` >= NOW()
      AND `date`  < NOW() + INTERVAL 10 DAY;

You could get both at once, but it could be inconvenient if you need to separate them:

SELECT * FROM events
    WHERE `date` >= NOW() - INTERVAL 10 DAY
      AND `date`  < NOW() + INTERVAL 10 DAY;

If you want to base on midnight instead of the current second, use CURDATE() instead of NOW().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.