MySQL: select date of current week's monday

前端 未结 7 1814
情书的邮戳
情书的邮戳 2020-12-06 12:10

I\'m building a weekly report using MySQL queries. First I get week number by

SELECT WEEK(CURDATE());

Then I need to displ

相关标签:
7条回答
  • 2020-12-06 12:58

    In fact, DATE_ADD is not necessary and complicates the logic. Here is the simplest version for "Date of the first day of the current week":

    select date(curdate() - interval weekday(curdate()) day)
    

    which would translate to: get the date() part of the (current date - an interval of N days) where N = today's number in the week, i.e. Thu is number 3 (for a week starting on Monday)

    Then, getting the Monday of previous week, is:

    select date(curdate() - interval weekday(curdate()) day - interval 1 week)
    

    which is more readable IMHO.

    0 讨论(0)
提交回复
热议问题