SELECT *FROM person_base_info;
小时维度查询
1、查询当前时间的前2小时之前的数据
select * from person_base_info WHERE create_time < DATE_SUB(NOW(), INTERVAL 2 HOUR)
2、查询当前时间7天之前的数据、7天之前之内的数据,刚好7天的数据
SELECT count(uuid) AS cont FROM person_base_info WHERE DATE(create_time) < DATE_SUB(CURDATE(), INTERVAL 7 DAY); SELECT count(uuid) AS cont FROM person_base_info WHERE DATE(create_time) > DATE_SUB(CURDATE(), INTERVAL 7 DAY); SELECT count(uuid) AS cont FROM person_base_info WHERE DATE(create_time) = DATE_SUB(CURDATE(), INTERVAL 7 DAY);
按天统计
-- 第一种方式
SELECT DATE(create_time) createTime,count(uuid) countNum FROM person_base_info
GROUP BY DATE(create_time) ORDER BY DATE(create_time) DESC;
-- 第二种方式
SELECT DATE_FORMAT(create_time,'%Y-%m-%d')createTime ,COUNT(uuid) countNum FROM person_base_info
GROUP BY DATE_FORMAT(create_time,'%Y-%m-%d')
按月统计
SELECT DATE_FORMAT(create_time,'%Y-%m')createTime ,COUNT(uuid)countNum FROM person_base_info GROUP BY DATE_FORMAT(create_time,'%Y-%m')
-- 另一种方式
SELECT MONTH(create_time) createTime,count(uuid) countNum FROM person_base_info
GROUP BY MONTH(create_time) ORDER BY MONTH(create_time) DESC;
按周统计
按当年当月统计
select month(create_time) months,COUNT(uuid)AS countNum from person_base_info where month(create_time) = month(curdate()) and year(create_time) = year(curdate())
按季度统计
-- 第一季度:1月-3月 第二季度:4月-6月 第三季度:7月-9月 第四季度:10月-12月 SELECT QUARTER(create_time) AS 季度,count(uuid) AS countNum FROM person_base_info GROUP BY QUARTER(create_time) ORDER BY QUARTER(create_time) DESC;
按年统计
SELECT DATE_FORMAT(create_time,'%Y')createTime ,COUNT(uuid)countNum FROM person_base_info GROUP BY DATE_FORMAT(create_time,'%Y')
--第二种方式 SELECT YEAR(create_time) createTime,count(uuid) countNum FROM person_base_info GROUP BY YEAR(create_time) ORDER BY YEAR(create_time) ASC;