I want to add every date of the year into my database table. How could I to do that ?
I was looking in google but there is no clue.
Please help me with some insert query to do that.
This is my table structure.
id | date
---------
1 | 2017-01-01
I want to add every date of the year into my database table. How could I to do that ?
I was looking in google but there is no clue.
Please help me with some insert query to do that.
This is my table structure.
id | date
---------
1 | 2017-01-01
Updated after question clarified.
Here is an updated version, try this out. For the while loop to work you need to create it as a stored procedure. Create the stored procedure like so
DELIMITER $$
create PROCEDURE insert_year_dates()
BEGIN
SET @t_current = NOW();
SET @t_end = DATE_ADD(NOW(), INTERVAL 1 YEAR);
WHILE(@t_current< @t_end) DO
INSERT INTO day (day) VALUES (@t_current);
SET @t_current = DATE_ADD(@t_current, INTERVAL 1 DAY);
END WHILE;
END;
Then call that stored procedure using
CALL insert_year_dates()
you can try it first get all date between to date and add it.
insert into day (date)
select * from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2017-01-01' and '2017-12-31'
In mysql you can choose the column to only be filled with a date format. You can insert the data as '2017' or '17'. It will work both fine.