DateTime group by date and hour

后端 未结 4 1477
无人共我
无人共我 2020-12-08 13:13

I have a datetime called activity_dt and the data looks like this:

2/5/2013 9:24:00 AM
2/7/2013 7:17:00 AM

How do i group

4条回答
  •  半阙折子戏
    2020-12-08 13:48

    Using MySQL I usually do it that way:

    SELECT count( id ), ...
    FROM quote_data
    GROUP BY date_format( your_date_column, '%Y%m%d%H' )
    order by your_date_column desc;
    

    Or in the same idea, if you need to output the date/hour:

    SELECT count( id ) , date_format( your_date_column, '%Y-%m-%d %H' ) as my_date
    FROM  your_table 
    GROUP BY my_date
    order by your_date_column desc;
    

    If you specify an index on your date column, MySQL should be able to use it to speed up things a little.

提交回复
热议问题