Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a temporary table with dates

Tags:

mysql

How can I create a temporary table and fill it with ceratin dates. I know the startdate and the "limit"

  • startdate = 2014-11-11
  • limit = 3

table should look like

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

I like to use this create table to join it with another

like image 569
Xaver Avatar asked Nov 19 '25 03:11

Xaver


1 Answers

You can generate the dynamic dates and then insert them int to the table as below. I have used a table instead of temporary table you can change it to temporary table.

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

Here is a demo

like image 182
Abhik Chakraborty Avatar answered Nov 20 '25 17:11

Abhik Chakraborty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!