MySQL显示范围之间的所有日期

时间:2013-03-05 11:48:36

标签: mysql date range

我想显示MySQL的fromto日期之间的所有日期。

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

修改

The answer here非常有帮助,但我还是没有完全得到所需的东西。在this sample中,它只能成功运行但不输出任何内容。而且我不知道这似乎是什么问题。

请帮忙。谢谢!

2 个答案:

答案 0 :(得分: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 with Demo

答案 1 :(得分:-3)

尝试从2013年3月13日开始,到目前为止是2013-3-20,

"SELECT * FROM schedule WHERE Field BETWEEN $fromdate AND $todate";