sql语句按照时间段查询

表结构 访问记录表(vs_records)   
  id                                           time
  0001                                   2005-12-1   14:12:16   
  0002                                   2005-12-1   14:22:16   
  0003                                   2005-12-1   14:47:16   
  0011                                 2005-12-1   14:36:16   
  0012                                 2005-12-2   10:22:16   
  0003                                   2005-12-2   10:23:16   

1、现在要展示每天24个小时的每5分钟的访问人数查询

select count(1),(datediff(minute,'2015-4-1', t.time)) dd   from vs_records t where  group by (datediff(minute,'2015-4-1', t.time)-1)/5

这儿用到了一个函数datediff查看函数的用法。 我的做法是先按分钟把时间分段再求余数这样将把5分钟的分组出来。这样就实现了。

2.按照月来统计

select count(1),month(t.time) year  from vs_records t where  group by month( t.time)

原文地址:https://www.cnblogs.com/cpx123/p/7727087.html