Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL SELECT last few days?

Tags:

sql

mysql

I was playing with MYSQL and I know there's a limit command that shows a certain amount of results, but i was wondering if MySQL alone can show only the last 3 days or something. Just wondering.

Update: I used NOW() to store times.

like image 268
Strawberry Avatar asked Nov 11 '09 04:11

Strawberry


People also ask

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 in SQL?

SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).

What is Date_add in MySQL?

The DATE_ADD() function adds a time/date interval to a date and then returns the date.


1 Answers

Use for a date three days ago:

WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -3 DAY); 

Check the DATE_ADD documentation.

Or you can use:

WHERE t.date >= ( CURDATE() - INTERVAL 3 DAY ) 
like image 176
OMG Ponies Avatar answered Sep 29 '22 01:09

OMG Ponies