如何在 mysql 中按小时总结
how to sum up by hour in mysql
比如我有一个table1(这只是table1的一部分,table1实际上有57000条记录,开始时间从2014-04-08 23:59:56 至 2014-04-09 23:59:56 ):
+---------------------+
| StartTime |
+---------------------+
| 2014-04-08 23:59:56 |
| 2014-04-08 23:59:59 |
| 2014-04-09 00:00:03 |
| 2014-04-09 00:00:03 |
| 2014-04-09 00:00:05 |
| 2014-04-09 00:00:08 |
| 2014-04-09 00:00:09 |
| 2014-04-09 00:00:11 |
| 2014-04-09 00:00:11 |
| 2014-04-09 00:00:17 |
+---------------------+
这个table是关于网络流的,开始时间是指每个流的开始时间's.Some网络流有相同的开始时间。我想计算每小时有多少流量。
伪代码就像
select count(starttime) as totalPerHour from table1 group by hour;
(just pseudo-code)
The result should be:
TimestampHOUR totalPerHour
2014-04-09 01:00 46
2014-04-09 02:00 3
2014-04-09 03:00 55
2014-04-09 04:00 12
2014-04-09 05:00 2369
2014-04-09 06:00 99
MySQL 有 hour(..)
。像这样使用它:
select count(*) as totalPerHour from table1 group by HOUR(StartTime);
合并 Hour(...) 和 Count(..)
select hour(StartTime), count(StartTime)
from table1
group by hour(StartTime)
比如我有一个table1(这只是table1的一部分,table1实际上有57000条记录,开始时间从2014-04-08 23:59:56 至 2014-04-09 23:59:56 ):
+---------------------+
| StartTime |
+---------------------+
| 2014-04-08 23:59:56 |
| 2014-04-08 23:59:59 |
| 2014-04-09 00:00:03 |
| 2014-04-09 00:00:03 |
| 2014-04-09 00:00:05 |
| 2014-04-09 00:00:08 |
| 2014-04-09 00:00:09 |
| 2014-04-09 00:00:11 |
| 2014-04-09 00:00:11 |
| 2014-04-09 00:00:17 |
+---------------------+
这个table是关于网络流的,开始时间是指每个流的开始时间's.Some网络流有相同的开始时间。我想计算每小时有多少流量。 伪代码就像
select count(starttime) as totalPerHour from table1 group by hour;
(just pseudo-code)
The result should be:
TimestampHOUR totalPerHour
2014-04-09 01:00 46
2014-04-09 02:00 3
2014-04-09 03:00 55
2014-04-09 04:00 12
2014-04-09 05:00 2369
2014-04-09 06:00 99
MySQL 有 hour(..)
。像这样使用它:
select count(*) as totalPerHour from table1 group by HOUR(StartTime);
合并 Hour(...) 和 Count(..)
select hour(StartTime), count(StartTime)
from table1
group by hour(StartTime)