时间:2021-07-01 10:21:17 帮助过:5人阅读
2、业务统计查询也按上述日期查询给统计日期和数量设置别名
SELECT FROM_UNIXTIME(m.sdate, ‘%Y-%m-%d‘) as date_str, count( * ) as date_count from t_table1 as m group by FROM_UNIXTIME(m.sdate, ‘%Y-%m-%d‘)
3、把两个查询用左连接合起,没数量的日期填0
SELECT t1.date_str, COALESCE(t2.date_total_count, 0) as date_total_count FROM( SELECT @cdate: = date_add(@cdate, interval - 1 day) as date_str FROM(SELECT @cdate: = date_add(CURDATE(), interval + 1 day) from t_table1) tmp1 WHERE @cdate > ‘2018 - 12 - 01‘ ) t1 LEFT JOIN( SELECT FROM_UNIXTIME(m.sdate, ‘%Y-%m-%d‘) as date_str, count(*) as date_total_count FROM t_table1 as m WHERE m.sdate between XXXX and XXXXX GROUP BY FROM_UNIXTIME(m.sdate, ‘%Y-%m-%d‘) ) t2 on t1.date_str = t2.date_str
查询结果如下图所示:
MySQL实现按天分组统计,提供完整日期列表,无数据自动补0
标签:sele val day nec 时间 分组 日期字段 oal color