mysql 如何时间分组,选择MySQL中的日期/时间分组按小时分组

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"

2010-02-13 2:12:00, "soma data"

I want to select a report which shows the data grouped by hour, for example:

On Feb 13, during the hour from 1:00 pm to 1:59 pm, there were 3 data points.

On Feb 13, during the hour from 2:00 pm to 2:59 pm, there was 1 data points.

...

Basically i want to report the cumulative amount of records which occurred during every hour of the day. So the end result would give me a report of say 10 days, broken out in 24 hour increments, so I can see how much data there is during any given hour on any given day.

TIA, Hope you can help!

解决方案

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值