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....
METHOD 1 : Using LIMIT clause in descending orderof specified rows from specifies row. We will retrieve last 5 rows in descending order using LIMIT and ORDER BY clauses and finally make the resultant rows ascending. Since Employee table has IDs, we will perform ORDER BY ID in our query.
We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.
How do I find last 30 days in SQL? SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).
We can use the ORDER BY statement and LIMT clause to extract the last data. The basic idea is to sort the sort the table in descending order and then we will limit the number of rows to 1. In this way, we will get the output as the last row of the table. And then we can select the entry which we want to retrieve.
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
Have you tried between
SELECT name,
date
from Lineas
WHERE date BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 DAY) AND CURDATE()
ORDER BY date DESC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With