SQL group by date, but get dates w/o records too

后端 未结 6 1398
臣服心动
臣服心动 2021-01-07 06:06

Is there an easy way to do a GROUP BY DATE(timestamp) that includes all days in a period of time, regardless of whether there are any records associated with th

6条回答
  •  清歌不尽
    2021-01-07 06:43

    Assuming you have more orders than dates something like this could work:

    select date, count(id) as orders
    from
    (
      SELECT DATE_ADD('2008-01-01', INTERVAL @rn:=@rn+1 DAY) as date from (select @rn:=-1)t, `order` limit 365
    ) d left outer join `order` using (date)
    group by date
    

提交回复
热议问题