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

后端 未结 8 1442
猫巷女王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条回答
  •  Happy的楠姐
    2020-11-30 04:27

    Week starts day from sunday then get First date of the Week and Last date of week

    SELECT  
      DATE("2019-03-31" + INTERVAL (1 - DAYOFWEEK("2019-03-31")) DAY) as start_date,  
      DATE("2019-03-31" + INTERVAL (7 - DAYOFWEEK("2019-03-31")) DAY) as end_date
    

    Week starts day from Monday then get First date of the Week and Last date of week

    SELECT  
      DATE("2019-03-31" + INTERVAL ( - WEEKDAY("2019-03-31")) DAY) as start_date, 
      DATE("2019-03-31" + INTERVAL (6 - WEEKDAY("2019-03-31")) DAY) as end_date
    

提交回复
热议问题