Mysql按天分组统计数据,当天没有数据则显示为0

参考文章:https://wsdtq123.iteye.com/blog/2270169

SELECT
	count( sno ) count,
	DATE( date ) signtime 
FROM
	(#           构造当前日期之前31天的日期表
	SELECT @cdate := date_add(@cdate, INTERVAL - 1 DAY) date
                 FROM (SELECT @cdate := date_add(CURRENT_DATE, INTERVAL 1 DAY) FROM student LIMIT 31) a
	) t_date
	LEFT JOIN signin signin ON DATE( signin.signtime ) = t_date.date 
WHERE
	to_days( now( ) ) - to_days( date ) <= 7 
GROUP BY
	to_days( date )

Mysql按天分组统计数据,当天没有数据则显示为0_第1张图片

你可能感兴趣的:(杂记)