In this answer I will outline how to generate your calendar tables.
Create three tables for days, hours and minutes:
CREATE TABLE days (
day DATE,
PRIMARY KEY (day)
)
CREATE TABLE hours (
hour INT,
PRIMARY KEY (hour)
)
CREATE TABLE minutes (
minute INT,
PRIMARY KEY (minute)
)
Fill the hours table with the numbers from 0 to 23 and the minutes table with the numbers from 0 to 59. To fill the days table, you can create a procedure like the following:
CREATE PROCEDURE make_days(IN start_date DATE, IN end_date DATE)
BEGIN
DECLARE curr_date DATE;
SET curr_date = start_date;
WHILE curr_date <= end_date DO
INSERT IGNORE INTO days(day) VALUES(curr_date);
SET curr_date = DATE_ADD(curr_date, INTERVAL 1 DAY);
END WHILE;
END
You can then call this procedure to create days like this:
CALL make_days('2011-01-01','2012-12-31');
Now you can create values for every minute in a given time interval using a query similar to the following:
SELECT YEAR(day) AS year, MONTH(day) AS month, DAYOFMONTH(day) AS day, hour, minute
FROM days, hours, minutes
WHERE CAST(CONCAT(day,' ',hour,':',minute) AS DATETIME) BETWEEN '2011-08-31 22:00' AND '2011-09-01 10:00'
ORDER BY year, month, day, hour, minute
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…