Kioko Kiaza Kioko Kiaza - 25 days ago 7
MySQL Question

Select all data from the last 5 days

In mysql I need to obtain all the last 5 days records.
So if I have

Name date
aaaa 20/11/2010
dddd* 24/11/2010*
bbbb 22/11/2010
cccc 23/11/2010
eeee* 25/11/2010*
ffff* 26/11/2010*


I need only the last 5 days records.

I tried something like:

SELECT name,date
from Lineas
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 5 DAY)
ORDER BY date DESC


but it isnĀ“t working....

Answer

If the problem is "records from the future" then you simply need to restrain your results a bit more than you've already done:

SELECT name,date 
from Lineas
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 5 DAY) AND date <= CURDATE()
ORDER BY date DESC