Home  >  Article  >  Backend Development  >  mysql时间段统计问题

mysql时间段统计问题

WBOY
WBOYOriginal
2016-06-06 20:33:121263browse

我的数据库是mysql的 时间字段类型是Timestamp
这个数据库每隔一段时间就会插入一条记录,类似下面的:

<code>id  |       datetime       |  data01  |  data02
1   | 2015-06-10 00:00:00  |   23.8   |   33.7
2   | 2015-06-10 00:01:11  |   21.8   |   12.7
3   | 2015-06-10 00:01:30  |   21.8   |   33.7
4   | 2015-06-10 01:02:00  |   23.8   |   33.7
5   | 2015-06-10 01:03:10  |   23.8   |   33.7
......
209 | 2015-06-10 23:03:00  |   23.8   |   33.7
</code>

我现在想分24小时统计,比如00:00:00-01:00:00的所有数据的总和,以此类推,还有就是能不能只控制时间,不要把年月日也算进去,只要时间区间,
感谢各位!

回复内容:

我的数据库是mysql的 时间字段类型是Timestamp
这个数据库每隔一段时间就会插入一条记录,类似下面的:

<code>id  |       datetime       |  data01  |  data02
1   | 2015-06-10 00:00:00  |   23.8   |   33.7
2   | 2015-06-10 00:01:11  |   21.8   |   12.7
3   | 2015-06-10 00:01:30  |   21.8   |   33.7
4   | 2015-06-10 01:02:00  |   23.8   |   33.7
5   | 2015-06-10 01:03:10  |   23.8   |   33.7
......
209 | 2015-06-10 23:03:00  |   23.8   |   33.7
</code>

我现在想分24小时统计,比如00:00:00-01:00:00的所有数据的总和,以此类推,还有就是能不能只控制时间,不要把年月日也算进去,只要时间区间,
感谢各位!

<code>sql</code><code>select count (*) from tablename where datetime between '2015-06-10 00:00:00' and '2015-06-10 01:00:00';
</code>

很简单啊

<code>sql</code><code>select substring(`datetime`,12,2) as `hour`,count(*) as `total` from `table` group by `hour`;
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn