Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all data from the last 5 days

Tags:

sql

mysql

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....

like image 499
Kioko Kiaza Avatar asked Dec 23 '10 13:12

Kioko Kiaza


People also ask

How do I select the last 5 records of a SQL table?

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.

How do I get last 7 days data in SQL 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 get last 30 days data in SQL?

How do I find last 30 days in SQL? SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).

How do I select last data entry in SQL?

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.


2 Answers

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
like image 118
CSkau Avatar answered Oct 12 '22 09:10

CSkau


Have you tried between

SELECT  name,
        date  
from    Lineas 
WHERE   date BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 DAY) AND CURDATE()
ORDER BY date DESC 
like image 33
Adriaan Stander Avatar answered Oct 12 '22 08:10

Adriaan Stander