How can I insert incremental dates in MySQL?

How to add rows to a table using only SQL, which cyclically changes the date, for example:

INSERT INTO my_table (the_date) VALUES ('2013-04-13'); INSERT INTO my_table (the_date) VALUES ('2013-04-14'); INSERT INTO my_table (the_date) VALUES ('2013-04-15'); INSERT INTO my_table (the_date) VALUES ('2013-04-16'); ... 

I need to insert a row for each day from 2013-05-07 for the next, for example. 1000 days.

+4
source share
2 answers

Something like this will do this: -

 INSERT INTO my_table (the_date) SELECT ADDDATE('2013-04-13', INTERVAL SomeNumber DAY) FROM (SELECT a.i+bi*10+ci*100+di*1000 AS SomeNumber FROM integers a, integers b, integers c, integers d) Sub1 WHERE SomeNumber BETWEEN 0 AND 1000 

It is based on a table called integers, with one column i with 10 rows, values ​​from 0 to 9.

The Between clause is available there, so you can limit the range of numbers added to the date.

+4
source

Try this code:

 $starDate = new DateTime('2013-05-07'); for ($i=0; $i < 1000; $i++) { $consulta ="INSERT INTO my_table (the_date) VALUES ('".date_format($starDate, 'Ym-d')."');"; $starDate = date_add($starDate, date_interval_create_from_date_string('1 days')); echo $consulta."</br>"; //try somthing like mysqli_query($consulta); } 

With php and mysqli .... you can do it with pure sql too;)

I give you this way.

Saludos;)

0
source

All Articles