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

后端 未结 6 1355
臣服心动
臣服心动 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:32

    There's a pretty straightforward way to do this… except that I can't remember it. But I adapted this query from this thread:

    SELECT 
        DISTINCT(LEFT(date_field,11)) AS `Date`,
        COUNT(LEFT(date_field,11)) AS `Number of events`
    FROM events_table
    GROUP BY `Date`
    

    It works in MySQL too

提交回复
热议问题