mysql统计当前月和近30天每天的数据


1,当前月

SELECT DATE_FORMAT(check_time,'%Y-%m-%d') as time, COUNT(*) FROM dw_rule WHERE check_state = 3 AND 
DATE_FORMAT( check_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) GROUP BY time

2,最近30天,不包括当天

因为mybatis里面用<=会报错,所以加上CDATA区

SELECT DATE_FORMAT(check_time,'%Y-%m-%d') as time, COUNT(*) FROM dw_rule WHERE check_state = 3 
<![CDATA[ AND DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= check_time ]]> GROUP BY time

 

表中时间为日期格式

DATE_FORMAT(create_time,'%Y-%m-%d') as time

表中时间为时间戳格式

FROM_UNIXTIME(create_time,'%Y-%m-%d') as time


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM