Home > database >  How to get all entries for 4 weeks sql?
How to get all entries for 4 weeks sql?

Time:12-20

How do I get all records from a table from this week and the next 3 weeks, in total 4 weeks. This will gave me this week.

SELECT *, FROM_UNIXTIME(date) as time  FROM `events` WHERE 
WEEK(FROM_UNIXTIME(date)) = WEEK(NOW())
AND MONTH(FROM_UNIXTIME(date)) = MONTH(NOW())
AND YEAR(FROM_UNIXTIME(date)) = YEAR(NOW()) 

Any ideas on how to get the data for the next 3 weeks.

CodePudding user response:

Use a time range:

WHERE date BETWEEN 
    UNIX_TIMESTAMP(DATE_ADD(CURRENT_DATE(), INTERVAL(1-DAYOFWEEK(CURRENT_DATE())) DAY)) -- beginning of current week
    AND UNIX_TIMESTAMP(DATE_ADD(CURRENT_DATE(), INTERVAL(1-DAYOFWEEK(CURRENT_DATE())   28) DAY)) -- beginning of 4 weeks after
  • Related