欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL格式化时间戳 统计当日,第二天,第三天,3个工作日以后的数据

程序员文章站 2022-07-11 17:15:28
mysql 查询出来的处理时间和开始时间都是13位的时间戳 SELECT `END_TIME`,`CREATE_TIME` FROM t_table 需求是统计当日,第二天,第三天,3个工作日以后的时间段的数据,真的是找了好多资料,真的是要记录一下如何转到天数去计算 首先 要把时间的戳给去掉(因为j ......

mysql 查询出来的处理时间和开始时间都是13位的时间戳

select `end_time`,`create_time` from t_table

MySQL格式化时间戳 统计当日,第二天,第三天,3个工作日以后的数据

需求是统计当日,第二天,第三天,3个工作日以后的时间段的数据,真的是找了好多资料,真的是要记录一下如何转到天数去计算

首先

要把时间的戳给去掉(因为java的date默认精度是毫秒,也就是说生成的时间戳就是13位的),并四舍五入一下

select round(eor.`end_time` / 1000),  round( eor.`create_time`/ 1000)from t_table

MySQL格式化时间戳 统计当日,第二天,第三天,3个工作日以后的数据

 

但是还是没有看到具体的时间,mysql需要用from_unixtime()函数格式化一下,具体用法百度很多 ,我也就不写了

select from_unixtime(round( eor.`end_time`/ 1000)), from_unixtime(round( eor.`create_time`/ 1000)) from t_table

MySQL格式化时间戳 统计当日,第二天,第三天,3个工作日以后的数据

 

终于看到具体的时间啦,但是,怎么计算天数呢?翻了百度mysql中的to_days()函数,可以返回天数,真好

select to_days(from_unixtime(round( eor.`end_time`/ 1000))), to_days(from_unixtime(round( eor.`create_time`/ 1000))) from t_table

MySQL格式化时间戳 统计当日,第二天,第三天,3个工作日以后的数据

 

这样 返回的是0到2019-06-05 之间的天数

最后

最后

select
count(to_days(from_unixtime(round(eor.`end_time` / 1000)))-to_days(from_unixtime(round( eor.`create_time`/ 1000)))<1 or null) as today,//小于1就是当天的
count(to_days(from_unixtime(round(eor.`end_time` / 1000)))-to_days(from_unixtime(round( eor.`create_time`/ 1000)))=1 or null) as secondday,//等于1就是第二个工作日count(to_days(from_unixtime(round(eor.`end_time` / 1000)))-to_days(from_unixtime(round( eor.`create_time`/ 1000)))=2 or null) as thirdday,//等于2就是第三个工作日
count(to_days(from_unixtime(round(eor.`end_time` / 1000)))-to_days(from_unixtime(round( eor.`create_time`/ 1000)))>2 or null) as afterthreedays//大于2  就是3个工作日以后的数据
from
t_table

 MySQL格式化时间戳 统计当日,第二天,第三天,3个工作日以后的数据

 

这就是我想要的结果啦!