How to populate a table with a range of dates?

后端 未结 10 1416
夕颜
夕颜 2020-11-22 04:24

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 po

10条回答
  •  没有蜡笔的小新
    2020-11-22 05:17

    If you have a table with a large enough contiguous set of ids you could use -

    INSERT INTO tablename (_date)
    SELECT '2011-01-01' + INTERVAL (id - 1) DAY
    FROM some_table_with_lots_of_ids
    WHERE id BETWEEN 1 AND 365
    

    note: but be aware that this might get you in trouble during leap-years (having 366 days)

提交回复
热议问题