How do I get the first day of the week of a date in mysql?

后端 未结 8 1437
猫巷女王i
猫巷女王i 2020-11-30 03:47

Suppose I have 2011-01-03 and I want to get the first of the week, which is sunday, which is 2011-01-02, how do I go about doing that?

The reason is I have this quer

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 04:28

    If the week starts on Sunday do this:

    DATE_ADD(mydate, INTERVAL(1-DAYOFWEEK(mydate)) DAY)
    

    If the week starts on Monday do this:

    DATE_ADD(mydate, INTERVAL(-WEEKDAY(mydate)) DAY);
    

    more info

提交回复
热议问题