参考:
http://c.biancheng.net/mysql/function/(编程网:MySQL函数大全)
https://www.cnblogs.com/lazyInsects/p/8006388.html(mysql统计数据)
https://www.cnblogs.com/poloyy/p/12890763.html(常用函数汇总)
一,时间查询
/*查询2小时前的数据*/ select * from tableName WHERE create_time < DATE_SUB(NOW(), INTERVAL 2 HOUR) #7天前 SELECT count(id) FROM rd_track_info WHERE DATE(create_time) < DATE_SUB(CURDATE(), INTERVAL 7 DAY);
二,统计数据
/*按天统计*/ SELECT count(id) countNum, DATE(create_time) createTime FROM rd_track_info GROUP BY DATE(create_time) ORDER BY DATE(create_time) DESC; /*按周统计*/ SELECT count(id) countNum, WEEK(create_time) createTime FROM rd_track_info GROUP BY WEEK(create_time) ORDER BY WEEK(create_time) DESC; /*按月统计*/ SELECT count(id) countNum, MONTH(create_time) createTime FROM rd_track_info GROUP BY MONTH(create_time) ORDER BY MONTH(create_time) DESC; /*按季度统计*/ SELECT count(id) countNum, QUARTER(create_time) createTime FROM rd_track_info GROUP BY QUARTER(create_time) ORDER BY QUARTER(create_time) DESC; /*按年统计*/ SELECT count(id) countNum, YEAR(create_time) createTime FROM rd_track_info GROUP BY YEAR(create_time) ORDER BY YEAR(create_time) DESC;