业务背景
统计各机型最近7天bug数量来支撑一张图表:
sql需要查询最近七天数据并按每天和机型进行分组
思路
1. 查询最近7天的数据
select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);
拓展
查询最近一天的数据
select * from table where to_days(column_time) = to_days(now());
select * from table where date(column_time) = curdate();
查询最近一个月的数据
select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time);
2. 查询需要的列并分组
例如:
SELECT
DATE_FORMAT( create_time, \'%Y-%m-%d\' ) days,
phone_model ,
count(*) count
FROM
( SELECT * FROM trial_feedback_pdm WHERE DATE_SUB( CURDATE( ), INTERVAL 7 DAY ) <= date( create_time) ) as day
GROUP BY
days,phone_model;
参考链接: