I need a MySQL table to hold ALL DATES between 2011-01-01 and 2011-12-31. I have created a table with one column names "_date", type DATE.
With what query can I populate the table with all the desired dates (instead of having to enter them by hand)?
if you're in a situation like me where procedures are prohibited, and your sql user does not have permissions for insert, therefore insert not allowed, but you want to generate a list of dates in a specific period, say current year to do some aggregation, use this
Try this:
Here's the SQL Fiddle to play with it: http://sqlfiddle.com/#!2/65d13/1
EDIT (to check if date already exists) as asked by Andrew Fox.
Here's the SQL Fiddle to play with it: http://sqlfiddle.com/#!2/66f86/1
I found this paste-and-go variant working:
I recently had a need to create a
calendar_date
table as below:I then populated it with all possible dates between
January 1, 2001
andDecember 31, 2100
(both inclusive) using the query below:On my local MySQL database, the
INSERT
query took just a few seconds. Hope this helps someone.