Select date/time groupings in MySQL grouped by hour

前端 未结 2 751
你的背包
你的背包 2020-12-19 05:48

I have a bunch of date data in a mysql table like this:

2010-02-13 1:00:00, \"soma data\"
2010-02-13 1:25:00, \"soma data\"
2010-02-13 1:37:00, \"soma data\"         


        
相关标签:
2条回答
  • 2020-12-19 05:57

    You may want to use the GROUP BY clause as in the following query:

    SELECT 
        COUNT(*),
        YEAR(dateTimeField),
        MONTH(dateTimeField),
        DAY(dateTimeField),
        HOUR(dateTimeField)
    FROM 
        yourTable
    WHERE
        dateTimeField >= '2010-02-04 00:00:00' AND
        dateTimeField < '2010-02-14 00:00:00'
    GROUP BY 
        YEAR(dateTimeField), 
        MONTH(dateTimeField),
        DAY(dateTimeField),
        HOUR(dateTimeField);
    

    You may also want to check the MySQL documentation for further reference on date and time functions:

    • MySQL 5.1 Reference Manual - Date and Time Functions
    0 讨论(0)
  • 2020-12-19 05:59
    SELECT HOUR(datetime), COUNT(*)
    FROM table
    GROUP BY HOUR(datetime);
    

    You will get two fields: the hour and the number of events during that hour.

    0 讨论(0)
提交回复
热议问题