MySQL: Select data from a table where the date falls in the current Week and current Month

前端 未结 3 914
孤城傲影
孤城傲影 2021-01-24 19:34

I am creating a web app where if a user clicks on Link called \"WEEK\", the page shows up all the posts which were submitted in that week. There\'s also an option to view all th

3条回答
  •  被撕碎了的回忆
    2021-01-24 19:41

    Well, by slightly modifying your code, you could realize the WEEK- and MONTH-Problem using SQL!

    Regarding to: MySQL-Documentation, this might be the solution for you:

    SELECT *
    FROM   posts
    WHERE  YEARWEEK(post_date) = YEARWEEK(NOW())
    

    Second one may be a bit more complicated:

    SELECT *
    FROM   posts
    WHERE  YEAR(post_date) = YEAR(NOW())
    AND    MONTH(post_date) = MONTH(NOW())
    

    I hope, i could help you.

提交回复
热议问题