mysql如何查询近一周的数据
作者:暮色微凉
时间:2023-04-29
浏览:6
在mysql中,可以利用select语句查询近一周的数据,语法为“select*fromtablewhereDATE_SUB(CURDATE(),INTERVAL7DAY)
本文目录
在mysql中,可以利用select语句查询近一周的数据,语法为“select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);”。
本教程操作环境:windows10系统、mysql8.0.22版本、Dell G3电脑。
mysql怎么查询近一周的数据
语法如下:
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);
示例如下:
效果如图(这里SQL语句中的一周范围是指星期一到星期日,星期一为一周的第一天,因是8月11日查询的,所以只显示星期一到星期六的结果):

日历:

简单来说就是用今天的日期生成前七天的日期(利用union all命令),并根据星期一的日期条件刷选出本周的日期
SELECT DATE(subdate(curdate(),date_format(curdate(),'%w')-1)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 1 day)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 2 day)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 3 day)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 4 day)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 5 day)) as thisweek union all SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 6 day)) as thisweek

解析:
SELECT DATE(subdate(curdate(),date_format(curdate(),’%w’)-1))
得到的是这周的第一天(星期一到星期天为一周);也即8月6日
作者最新文章
如何正确使用夸克网盘前缀格式?夸克网盘前缀使用教程
2026-11-24 09:36
抖音官方充值链接及入口指南
2026-11-23 10:24
AO3防走失官网链接2025手机版入口
2026-11-22 16:21
抖音搜索app是做什么的 抖音搜索app收费吗
2026-11-19 15:26
腾讯会议本地录制和云录制怎么选 保存路径设置
2026-11-18 17:23
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































