mysql How to get all entries for 4 weeks sql?

zlhcx6iw  于 2022-12-22  发布在  Mysql
关注(0)|答案(1)|浏览(103)

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.

vuktfyat

vuktfyat1#

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

相关问题