当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL按时间统计数据的方法总结

MySQL按时间统计数据的方法总结

2019年03月18日  | 移动技术网IT编程  | 我要评论

在做数据库的统计时,经常会需要根据年、月、日来统计数据,然后配合echarts来制作可视化效果。

数据库:mysql

思路

按照时间维度进行统计的前提是需要数据库中有保留时间信息,建议是使用mysql自带的datetime类型来记录时间。

`timestamp` datetime default null,

在mysql中对于时间日期的处理的函数主要是date_format(date,format)。可用的参数如下

格式 描述
%a 缩写星期名
%b 缩写月名
%c 月,数值
%d 带有英文前缀的月中的天
%d 月的天,数值(00-31)
%e 月的天,数值(0-31)
%f 微秒
%h 小时 (00-23)
%h 小时 (01-12)
%i 小时 (01-12)
%i 分钟,数值(00-59)
%j 年的天 (001-366)
%k 小时 (0-23)
%l 小时 (1-12)
%m 月名
%m 月,数值(00-12)
%p am 或 pm
%r 时间,12-小时(hh:mm:ss am 或 pm)
%s 秒(00-59)
%s 秒(00-59)
%t 时间, 24-小时 (hh:mm:ss)
%u 周 (00-53) 星期日是一周的第一天
%u 周 (00-53) 星期一是一周的第一天
%v 周 (01-53) 星期日是一周的第一天,与 %x 使用
%v 周 (01-53) 星期一是一周的第一天,与 %x 使用
%w 星期名
%w 周的天 (0=星期日, 6=星期六)
%x 年,其中的星期日是周的第一天,4 位,与 %v 使用
%x 年,其中的星期一是周的第一天,4 位,与 %v 使用
%y 年,4 位
%y 年,2 位

注:当涉及到按日统计是,需要使用%j,而如果使用%d, %e, %w的话,那么不同月份/周里的相同值会统计在一起。

涉及到获取当前时间,则可以通过now()或者sysdate()来获取。

select sysdate() from dual;

select now() from dual;

按照实际需求使用group by查询即可。

结论
需统计的表结构如下:

create table `apilog` (

 `id` int(11) not null auto_increment,

 `username` varchar(64) default null,

 `action` varchar(64) default null,

 `params` text,

 `result` text,

 `timestamp` datetime default null,

 primary key (`id`)

)

统计时间范围内不同分类action的数量

# 当日

select action, count(id) count from apilog where date_format(`timestamp`,'%j') = date_format(now(),'%j') order by count desc;

# 当周

select action, count(id) count from apilog where date_format(`timestamp`,'%u') = date_format(now(),'%u') order by count desc;

# 当月

select action, count(id) count from apilog where date_format(`timestamp`,'%m') = date_format(now(),'%m') order by count desc;

# 当年

select action, count(id) count from apilog where date_format(`timestamp`,'%y') = date_format(now(),'%y') order by count desc;

统计某分类action的时间维度数量

# 按日

select action, date_format(`timestamp`,'%j'), count(id) count from apilog where action = 'xxx' group by date_format(`timestamp`,'%j')

# 按周

select action, date_format(`timestamp`,'%u'), count(id) count from apilog where action = 'xxx' group by date_format(`timestamp`,'%u')

# 按月

select action, date_format(`timestamp`,'%m'), count(id) count from apilog where action = 'xxx' group by date_format(`timestamp`,'%m')

# 按年

select action, date_format(`timestamp`,'%y'), count(id) count from apilog where action = 'xxx' group by date_format(`timestamp`,'%y')

同时按action和时间维度统计

# 按日

select action, date_format(`timestamp`,'%j'), count(id) count from apilog group by action, date_format(`timestamp`,'%j')

# 按周

select action, date_format(`timestamp`,'%u'), count(id) count from apilog group by action, date_format(`timestamp`,'%u')

# 按月

select action, date_format(`timestamp`,'%m'), count(id) count from apilog group by action, date_format(`timestamp`,'%m')

# 按年

select action, date_format(`timestamp`,'%y'), count(id) count from apilog group by action, date_format(`timestamp`,'%y')

以上就是比较常用的时间统计了,更多的时间维度,可以参考上面的参数表类似处理即可。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网