MySQL display all date in between range(MySQL显示范围内的所有日期)
问题描述
我想显示 MySQL 中 from 和 to 日期之间的所有日期.
I want to display all dates between a from and to dates from MySQL.
例如 schedule 表中具有 from 和 to 字段的数据是:
For example the data from schedule table that has from and to fields are:
from 日期为 2013-3-13,且
到日期是2013-3-20,
我想要的结果是:
2013-3-13
2013-3-14
2013-3-15
2013-3-16
2013-3-17
2013-3-18
2013-3-19
2013-3-20
如何仅使用 MySQL 查询来实现这一点(不必使用存储过程,因为我不熟悉它)?
How can I achieve this using MySQL query only (without having to use stored procedure 'cause I'm not familiar with it)?
这里的答案很有帮助,虽然我还没有完全明白想要什么.在this sample中,它只运行成功,没有输出任何东西.而且我不知道问题出在哪里.
The answer here is very helpful, though I still don't fully get what is desired. In this sample, it only runs successfully but doesn't output anything. And I don't know what seems to be the problem.
请帮忙.谢谢!
推荐答案
您可以使用以下方法来生成日期列表:
You can use the following to generate your list of dates:
select a.Date, s.*
from
(
select curdate() + INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
inner join schedule s
on a.Date >= s.fromDate
and a.Date <= s.toDate
请参阅 SQL Fiddle with Demo
这篇关于MySQL显示范围内的所有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL显示范围内的所有日期
基础教程推荐
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
