创建一个包含日期的临时表

时间:2014-11-15 10:15:27

标签: mysql

如何创建临时表并用ceratin日期填充它。我知道startdate和“limit”

  • startdate = 2014-11-11
  • 限制 = 3

表应该看起来像

2014-11-11
2014-11-12
2014-11-13

我喜欢使用此创建表将其与另一个

连接

1 个答案:

答案 0 :(得分:3)

您可以生成动态日期,然后将它们插入到表中,如下所示。我使用了一个表而不是临时表,你可以把它改成临时表。

CREATE TEMPORARY TABLE IF NOT EXISTS dates_test
(dates datetime);


insert into dates_test (dates)
select 
t1.date
from
(
  select
  a.Date as date
  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
  where a.Date BETWEEN '2014-11-11' 
  and
  DATE_ADD('2014-11-11' ,INTERVAL 3 DAY)
)t1

这是 demo