mysql获取今天每1小时或者每半小时,最近一周的数据统计

一:获取当日每隔一小时的数据记录

SELECT DATE_FORMAT(createtime, '%H') triggerDay, COUNT(rid) triggerCount
FROM zz_access
WHERE TO_DAYS(createtime) = TO_DAYS(NOW())
GROUP BY triggerDay
ORDER BY triggerDay ASC;

 

 

 

二:通用 ,获取每半小时的数据记录

SELECT HOUR(CREATETIME)AS H,FLOOR(MINUTE(CREATETIME)/30) AS M, COUNT(*) AS COUNT 
FROM ZZ_ACCESS
WHERE TO_DAYS(CREATETIME) = TO_DAYS(NOW())
GROUP BY FLOOR(MINUTE(CREATETIME)/30),HOUR(CREATETIME) 
ORDER BY H,M;

三:获取最近一周的统计记录

SELECT DATE_FORMAT(createtime, '%m-%d') days, COUNT(rid) counts
FROM zz_access
WHERE DATE_SUB(CURDATE(), INTERVAL 6 DAY) <= DATE(CREATETIME)
GROUP BY days
ORDER BY days ASC;


 

你可能感兴趣的:(数据库)