3

我想显示MySQL 中afrom和日期之间的所有日期。to

例如schedule,具有fromto字段的表中的数据是:

from日期是2013-3-13,并且

to日期是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 查询来实现这一点(不必使用存储过程,因为我不熟悉它)?

编辑:

这里的答案非常有帮助,尽管我仍然没有完全得到想要的东西。在这个示例中,它只运行成功但不输出任何内容。而且我不知道似乎是什么问题。

请帮忙。谢谢!

4

2 回答 2

8

您可以使用以下内容生成日期列表:

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

于 2013-03-05T13:13:36.607 回答
-4

尝试从日期开始是 2013-3-13,到日期是 2013-3-20,

"SELECT * FROM schedule WHERE Field BETWEEN $fromdate AND $todate";
于 2013-03-05T12:13:58.993 回答