mysql 按日、周、月、年统计sql语句整理，实现报表统计可视化

– –按日

SELECT COUNT(*),DATE(CreateTime) FROM t_voipchannelrecord WHERE YEAR(CreateTime)='2016' GROUP BY DAY(CreateTime)

– –按周

 SELECT COUNT(*),WEEK(CreateTime) FROM t_voipchannelrecord WHERE MONTH(CreateTime) = '8' GROUP BY WEEK(CreateTime) 

–周一到周五每天的统计结果

 SELECT COUNT(*),DAYNAME(CreateTime) FROM t_voipchannelrecord WHERE YEAR(CreateTime) = '2016' GROUP BY DAYNAME(CreateTime) 

–统计本周数据

SELECT COUNT(*) FROM t_voipchannelrecord WHERE MONTH(CreateTime) =
MONTH(CURDATE()) AND WEEK(CreateTime) = WEEK(CURDATE())

–按月统计

SELECT COUNT(*),MONTH(CreateTime) FROM t_voipchannelrecord WHERE YEAR(CreateTime) = '2016' GROUP BY MONTH(CreateTime) 

–统计本月数据

SELECT COUNT(*) FROM t_voipchannelrecord WHERE MONTH(CreateTime) =
MONTH(CURDATE()) AND YEAR(CreateTime) = YEAR(CURDATE())

–按季统计

SELECT COUNT(*),QUARTER(CreateTime) FROM t_voipchannelrecord WHERE YEAR(CreateTime) = '2016' GROUP BY QUARTER(CreateTime) 

–按年统计


SELECT COUNT(*),YEAR(CreateTime) FROM t_voipchannelrecord  GROUP BY YEAR(CreateTime) 

–时间段(该段参考：出处)

N天内记录
WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段) <= N

where date(时间字段)=date(now())
或
where to_days(时间字段) = to_days(now());

查询一周：
select * from table   where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);

查询一个月：
select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time);

Select * From user Where
DATE_FORMAT(birthday,'%m-%d') >= '06-03' and DATE_FORMAT(birthday,'%m-%d')
<= '07-08';

group by concat(date_format(savetime, '%Y '),FLOOR((date_format(savetime, '%m ')+2)/3))
或
select YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1,count(*)
from yourTable
group by YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1;


==未完待续

10-14 2.7万

12-28 2507

03-09 4301

10-16 2411

03-09 1.3万

11-24 1.4万

10-21 5704

12-04 2424

12-03 7734

01-14 2233