首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

查询某个时间段(跨日)内每小时的数据解决思路

2012-03-08 
查询某个时间段(跨日)内每小时的数据SQL codeselect type,count(*) from `table` where date 2012-02-

查询某个时间段(跨日)内每小时的数据

SQL code
select type,count(*) from `table` where date >= '2012-02-04 08:00:00' and date < '2012-02-05 08:00:00' group by type

以上例子是查询2012.02.04日8点到2012.02.05日8点的数据。如果要查询这个时间段内每小时的数据这个sql查询怎么写?

其中date字段格式是datetime。

[解决办法]
SQL code
select DATE_FORMAT(`date`,'%Y%m%d%H')  ,Count(*)from `table` where date >= '2012-02-04 08:00:00'  and  date < '2012-02-05 08:00:00' group by DATE_FORMAT(`date`,'%Y%m%d%H')
[解决办法]
select type,DATE_FORMAT(`date`,'%Y-%m-%d-%H'),count(*) from `table` where date >= '2012-02-04 08:00:00' and date < '2012-02-05 08:00:00' group by type,DATE_FORMAT(`date`,'%Y-%m-%d-%H')

热点排行