당신은 저장 프로 시저로 수행 할 수 있습니다
DELIMITER $$
CREATE PROCEDURE createDatesTable()
BEGIN
DECLARE FullDate date;
DROP TABLE IF EXISTS dates;
CREATE TABLE dates(
date_key date NOT NULL,
calendarYear int NOT NULL,
calendarQuarter tinyint NOT NULL,
calendarMonthNo int NOT NULL,
calendarWeekNo tinyint NOT NULL,
dayNumberOfMonth tinyint NOT NULL,
dayNumberOfWeek tinyint NOT NULL,
PRIMARY KEY (date_key));
SET FullDate = '2012-01-01';
WHILE (FullDate <= '2012-12-31') DO
BEGIN
INSERT INTO dates(
date_key,
calendarYear,
calendarQuarter,
calendarMonthNo,
calendarWeekNo,
dayNumberOfMonth,
dayNumberOfWeek
)VALUES(
FullDate,
YEAR(FullDate),
QUARTER(FullDate),
MONTH(FullDate),
WEEK(FullDate, 1), /*Have a look at the WEEK() function in the manual!!!*/
DAYOFMONTH(FullDate),
DAYOFWEEK(FullDate)
);
SET FullDate = DATE_ADD(Fulldate, INTERVAL 1 DAY);
END;
END WHILE;
END ;
$$
DELIMITER ;
는 그런 다음
call createDatesTable()
을하고 당신은 당신의 테이블을 채운 것입니다.
중요 : ypercube 님의 의견이 맞습니다. 이것을 고려해야합니다. 따라서 WEEK() function 및 지원되는 모드를 살펴보십시오. 그에 따라 절차를 조정하십시오.
당신은 주를 시작 할 때 먼저 정의해야합니다. 월요일에는? 일요일에? 1 월 1 일과 같은 날에? –