MySQL: How to select records for this week?(MySQL:如何选择本周的记录?)
问题描述
我在 sqlfiddle 上有带有结构的表 temp
:
I have table temp
with structure on sqlfiddle:
id(int 11 primary key)
name(varchar 100)
name2(varchar 100)
date(datetime)
我想获得本周的记录,例如如果现在 21.11.2013 我想要 18.11.2013 到 24.11.2013(每周)的所有行
I would like get record on this week, example if now 21.11.2013 i would like all rows on 18.11.2013 to 24.11.2013(on week)
现在我看到下一个算法:
Now I see next algorithm:
- 获取工作日
- 计算几天前是星期一
- 计算星期一的日期
- 计算未来的星期日
- 按日期提出请求
请告诉我,是否存在更短的算法(最好在查询 MySQL 中)?
Tell me please, is exist a shorter algorithm (preferably in the query MySQL)?
ADD 问题是: 为什么要这个查询 选择日期 17.11.2013(Sunday) - 23.11.2013(Saturday)
以及如何获取日期 18.11.2013(Monday) - 24.11.2013(Sunday) 的记录)
?
ADD Question is: Why this query select record on date 17.11.2013(Sunday) - 23.11.2013(Saturday)
and how get records on date 18.11.2013(Monday) - 24.11.2013(Sunday)
?
查询:
select * from temp
where yearweek(`date`) = yearweek(curdate())
谢谢!
推荐答案
使用 YEARWEEK()
:
SELECT *
FROM your_table
WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)
这篇关于MySQL:如何选择本周的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL:如何选择本周的记录?


- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01