Emil Engler
Emil Engler

Reputation: 565

MYSQL only get entries which are not older than X days

How can I fetch rows that are now older than three days?

The dates are stored in a format like this 2018-12-06 for the 6th of December 2018

Upvotes: 2

Views: 1146

Answers (2)

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 32003

You have an invalid condition. Try it like this:

 SELECT * FROM posts 
 where date(creation_date) >=DATE_ADD(CURDATE(), INTERVAL -3 DAY)
 and date(creation_date) <=curdate()
 ORDER BY id DESC

Here I used DATE_ADD function to go back 3 days

Upvotes: 0

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

Not tested but you need something like this,

SELECT * FROM posts WHERE creation_date >= (DATE(NOW()) - INTERVAL 3 DAY) ORDER BY id DESC

For mysql syntax and date function

Upvotes: 3

Related Questions