当前位置: 首页 > MySQL数据库

mysql如何查询近一周的数据

时间:2026-01-27 10:40:18

在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日


上一篇:mysql如何查询库中所有表
下一篇:vue指令的三要素有哪些
mysql
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素