MySQL查询几天内每天的数据
折线图需要查询到最近七天内的每天的数据以及近七天的日期返回给前端:
下面是之前的sql,得到的数量是总和,并不是某一天的具体数量
- 任务完成
这个SQL也可以得出哪一天有多少数量,但是如果某天没有数据他就不显示那一天了,不符合业务
SELECT DATE_FORMAT(CREATE_DATE,'%Y-%m-%d') as time,count(*) from visit_log
WHERE LOG_TYPE = 'VIEW' && LOG_MODULAR = 'CARD' and DEPT_ID = '2a8d6f4cbd564a14bcabb76447702052'
GROUP BY time;
经过查阅发现只需要简单的加上一个’ Group By 时间 ’ 进行分组就可以了,如下
代码片:
<select id="qryDeptViewShop" resultType="java.util.Map">
select a.click_date AS time, ifnull(b.count,0) as count
from (
SELECT date_sub(curdate(), interval 0 day) as click_date
union all
SELECT date_sub(curdate(), interval 1 day) as click_date
union all
SELECT date_sub(curdate(), interval 2 day) as click_date
union all
SELECT date_sub(curdate(), interval 3 day) as click_date
union all
SELECT date_sub(curdate(), interval 4 day) as click_date
union all
SELECT date_sub(curdate(), interval 5 day) as click_date
union all
SELECT date_sub(curdate(), interval 6 day) as click_date
) a left join (
SELECT date(log1.CREATE_DATE) as datetime ,COUNT(1) as count FROM visit_log log1
where log1.LOG_TYPE = 'PAY_SHOP' and log1.DEPT_ID = #{
deptId} group by datetime
) b on a.click_date = b.datetime order by time desc;
</select>
得出结果:
下面为练习markdown编辑器
之前的SQL
标记文本
删除文本
H2O is是液体。
210 运算结果是 1024.
图片:
居中的图片:
居中并且带尺寸的图片:
功能快捷键
撤销:Ctrl/Command + Z
重做:Ctrl/Co