在实际项目开发过程中,按不同时间维度查询业务数据的操作异常频繁。比较多的操作如支持按时间周期范围做列表数据的筛选,或者是按年月日等维度的图表展示,亦或者是首页的概况,三维大屏的展示等,都离不开不同时间周期查询。
列表台账数据的自定义时间范围查询功能,请点击这里查看详情。
以下是mysql按照不同时间周期做数据的分组统计,也可以适用于相关周期的数据查询。
需要注意的点:sql关键字的执行顺序,具体可以参看下方链接:
https://www.cnblogs.com/virgosnail/p/16061395.html
1.年周期
select count(sai.id) AS sumSesr,
DATE_FORMAT(sai.apply_time, '%Y') AS remindTimeStr
from supplies_apply_info sai
group by remindTimeStr
order by remindTimeStr ASC
2.季度周期
select count(sai.id) AS sumSesr,
CONCAT(YEAR(sai.apply_time), '-Q', QUARTER(sai.apply_time)) AS remindTimeStr
from supplies_apply_info sai
group by remindTimeStr
order by remindTimeStr ASC
3.月周期
select count(sai.id) AS sumSesr,
DATE_FORMAT(sai.apply_time, '%Y-%m') AS remindTimeStr
from supplies_apply_info sai
group by remindTimeStr
order by remindTimeStr ASC
4.周的周期
select count(sai.id) AS sumSesr,
CONCAT(YEAR(sai.apply_time), '-W', LPAD(WEEK(sai.apply_time), 2, '0')) AS remindTimeStr
from supplies_apply_info sai
group by remindTimeStr
order by remindTimeStr ASC
5.日周期
select count(sai.id) AS sumSesr,
DATE_FORMAT(sai.apply_time, '%Y-%m-%d') AS remindTimeStr
from supplies_apply_info sai
group by remindTimeStr
order by remindTimeStr ASC