/ Published in: SQL
creates a minimal calendar table
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
DROP TABLE IF EXISTS calendar_t; CREATE TABLE calendar_t ( id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, `date` DATE NOT NULL, DAY VARCHAR(9) NOT NULL, MONTH VARCHAR(13) NOT NULL, `year` INTEGER NOT NULL ); DROP VIEW IF EXISTS digits_v; CREATE VIEW digits_v AS SELECT 0 AS n 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 ; INSERT INTO calendar_t ( `date`, DAY, MONTH, `year` ) SELECT date_add('2015-01-01', INTERVAL 100*a2.n + 10*a1.n + a0.n DAY) AS `date`, dayname(date_add('2015-01-01', INTERVAL 100*a2.n + 10*a1.n + a0.n DAY)) AS DAY, monthname(date_add('2015-01-01', INTERVAL 100*a2.n + 10*a1.n + a0.n DAY)) AS MONTH, YEAR(date_add('2015-01-01', INTERVAL 100*a2.n + 10*a1.n + a0.n DAY)) AS `year` FROM digits_v a2 CROSS JOIN digits_v a1 CROSS JOIN digits_v a0 ORDER BY date_add('2015-01-01', INTERVAL 100*a2.n + 10*a1.n + a0.n DAY) ;