Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show records from now() till next 7 days in mysql

Tags:

mysql

I want to select rows from the datetime now till 7 days in the future, how can I do this? Read alot about the date function of mysql but cant figure it out, this is the MySQL code:

SELECT  id, date_format(datum, '%d/%m') AS date,
        date_format(datum, '%H:%i') AS time, date  
FROM wedstrijden
WHERE date >= now()
ORDER BY datum asc 

I have to do something with:

date >= now() till 7 days further
like image 252
Frank Avatar asked Jan 26 '11 15:01

Frank


People also ask

How do I get the last 7 days record in SQL Server?

To get yesterday's date, you need to subtract one day from today's date. Use GETDATE() to get today's date (the type is datetime ) and cast it to date . In SQL Server, you can subtract or add any number of days using the DATEADD() function. The DATEADD() function takes three arguments: datepart , number , and date .

What does NOW () return in MySQL?

The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS.

How do I subtract a date from 7 days in SQL?

If this is SQL Server, BETWEEN CONVERT(date, GETDATE()) AND DATEADD(DD, -7, CONVERT(date, GETDATE())) .


2 Answers

I would submit that the most elegant way would be:

WHERE `date` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)

Edit: this doc page is like the most useful thing ever. Bookmark it, because it is totally handy.

like image 174
TehShrike Avatar answered Oct 16 '22 07:10

TehShrike


You could use the INTERVAL modifier to add a week to the current time as follows:

...WHERE date >= NOW() AND date <= NOW() + INTERVAL 7 DAY;
like image 43
John Parker Avatar answered Oct 16 '22 07:10

John Parker