mysql日历表创建教程
程序员文章站
2022-07-07 23:26:32
首先我们先创建自己需要的日历表(先查询是否存在创建表):
drop table if exists kalendar_hour;
CREATE TABLE `kalenda...
首先我们先创建自己需要的日历表(先查询是否存在创建表):
drop table if exists kalendar_hour; CREATE TABLE `kalendar_hour` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `HOUR_SHORT_DESC` datetime DEFAULT NULL, `HOUR_LONG_DESC` varchar(50) DEFAULT NULL, `DAY_SHORT_DESC` datetime DEFAULT NULL, `DAY_LONG_DESC` varchar(50) DEFAULT NULL, `WEEK_DESC` varchar(20) DEFAULT NULL, `WEEK_ID` varchar(20) DEFAULT NULL, `WEEK_LONG_DESC` varchar(50) DEFAULT NULL, `MONTH_ID` varchar(20) DEFAULT NULL, `MONTH_LONG_DESC` varchar(50) DEFAULT NULL, `QUARTER_ID` varchar(20) DEFAULT NULL, `QUARTER_LONG_DESC` varchar(20) DEFAULT NULL, `YEAR_ID` varchar(20) DEFAULT NULL, `YEAR_LONG_DESC` varchar(50) DEFAULT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=10000000000 DEFAULT CHARSET=utf8;
创建过程:
查看全部存储过程:
show procedure status;创建过程,参数为开始日期和创建条数(或者是开始日期和结束日期):
drop procedure if exists pro_kalendar_hour; create procedure pro_kalendar_hour(in start_date VARCHAR(20),in date_count int) begin declare i int; set i=0; DELETE from kalendar_hour; while i < date_count DO INSERT into kalendar_hour (HOUR_SHORT_DESC,HOUR_LONG_DESC,DAY_SHORT_DESC,DAY_LONG_DESC,WEEK_DESC,WEEK_ID,WEEK_LONG_DESC,MONTH_ID,MONTH_LONG_DESC,QUARTER_ID,QUARTER_LONG_DESC,YEAR_ID,YEAR_LONG_DESC) SELECT DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d %H') HOUR_SHORT_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y年%m月%d日%H点') HOUR_LONG_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d') DAY_SHORT_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y年%m月%d日') DAY_LONG_DESC, case DAYOFWEEK(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s')) when 1 then '星期日' when 2 then '星期一' when 3 then '星期二' when 4 then '星期三' when 5 then '星期四' when 6 then '星期五' when 7 then '星期六' end WEEK_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y%u') WEEK_ID, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y年第%u周') WEEK_LONG_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y%m') MONTH_ID, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y年第%m月') MONTH_LONG_DESC, CONCAT(DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y'),quarter(STR_TO_DATE( start_date,'%Y-%m-%d %H:%i:%s'))) QUARTER_ID, CONCAT(DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y'),'年第',quarter(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s')),'季度') QUARTER_LONG_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y') YEAR_ID, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y年') YEAR_LONG_DESC from dual; set i=i+1; set start_date = DATE_FORMAT(date_add(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),interval 1 hour),'%Y-%m-%d'); end while; end;
然后执行过程就行了:
call pro_kalendar_hour('2015-01-01 00:00:00',438000);
如果是开始日期和结束日期:
drop procedure if exists pro_kalendar_hour; create procedure pro_kalendar_hour(in start_date VARCHAR(20),in end_date VARCHAR(20)) begin declare i int; set i=0; DELETE from kalendar_hour; while DATE_FORMAT(start_date,'%Y-%m-%d %H:%i:%s') < DATE_FORMAT(end_date,'%Y-%m-%d %H:%i:%s') DO INSERT into kalendar_hour (HOUR_SHORT_DESC,HOUR_LONG_DESC,DAY_SHORT_DESC,DAY_LONG_DESC,WEEK_DESC,WEEK_ID,WEEK_LONG_DESC,MONTH_ID,MONTH_LONG_DESC,QUARTER_ID,QUARTER_LONG_DESC,YEAR_ID,YEAR_LONG_DESC) SELECT DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d %H') HOUR_SHORT_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y年%m月%d日%H点') HOUR_LONG_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d') DAY_SHORT_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y年%m月%d日') DAY_LONG_DESC, case DAYOFWEEK(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s')) when 1 then '星期日' when 2 then '星期一' when 3 then '星期二' when 4 then '星期三' when 5 then '星期四' when 6 then '星期五' when 7 then '星期六' end WEEK_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y%u') WEEK_ID, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y年第%u周') WEEK_LONG_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y%m') MONTH_ID, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y年第%m月') MONTH_LONG_DESC, CONCAT(DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y'),quarter(STR_TO_DATE( start_date,'%Y-%m-%d %H:%i:%s'))) QUARTER_ID, CONCAT(DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y'),'年第',quarter(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s')),'季度') QUARTER_LONG_DESC, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y') YEAR_ID, DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y年') YEAR_LONG_DESC from dual; set i=i+1; set start_date = DATE_FORMAT(date_add(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),interval 1 hour),'%Y-%m-%d %H'); end while; end;
执行存储过程:
call pro_kalendar_hour('2015-01-01 00:00:00','2065-01-01 00:00:00');