MySQL2023-12-18 · 约 1 分钟
MySQL查询今日、昨日、近7天、近30天、近一年的数据
昨日
sql
select count(*) from access_log where DATEDIFF(create_time, NOW()) = -1;
今日
sql
select count(*) from access_log where to_days(create_time) = to_days(now());
近7天
sql
select count(*) from access_log where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(create_time);
近30天
sql
select count(*) from access_log where create_time >= DATE_SUB(NOW(), INTERVAL 30 DAY);
今年
sql
select count(*) from access_log where YEAR(create_time)=YEAR(NOW());
觉得有用就点个赞吧