建立表:
create table dic_date(
date date
,str_date varchar(16)
,local_date varchar(16)
,year int
,month int
,day int
,dayofyear int
,dayofmonth int
,weekofyear int
,weekofmonth int
,create_datetime timestamp default now() comment '資料建立日期'
) comment '日期字典表' ;
建立索引:
alter table dic_date add index index_dic_date_date (date);
alter table dic_date add index index_dic_date_str_date (str_date);
alter table dic_date add index index_dic_date_local_date (local_date);
字段取值測試:
select date_format('2018-01-01','%y-%m-%d') date ;
select concat(date_format('2018-01-01','%y-%m-%d'),'') str_date ;
select concat(year(date_format('2018-01-01','%y-%m-%d')),'年',month(date_format('2018-02-01','%y-%m-%d')),'月',day(date_format('2018-01-01','%y-%m-%d')),'日') local_date;
select year(date_format('2018-01-01','%y-%m-%d')) year;
select month(date_format('2018-02-01','%y-%m-%d')) month;
select day(date_format('2018-01-01','%y-%m-%d')) day;
select dayofyear(date_format('2018-01-01','%y-%m-%d')) day_in_year ;
select dayofmonth(date_format('2018-01-01','%y-%m-%d')) ;
select weekofyear(date_format('2018-01-08','%y-%m-%d')) ;
select week(date_format('2018-02-05','%y-%m-%d'), 5) - week(date_sub(date_format('2018-02-05','%y-%m-%d'), interval dayofmonth(date_format('2018-02-05','%y-%m-%d')) - 1 day), 5) + 1 ;
建立儲存過程(注意修改自己對應的資料庫庫名,不然會報錯哦):
delimiter $$
create
/*[definer = ]*/
procedure `test`.`pro_date_table`()
/*language sql
| [not] deterministic
| | sql security
| comment 'string'*/
begin
declare v_start_date date default date_format('2018-01-01','%y-%m-%d');
while v_start_date <= date_format('2025-12-31','%y-%m-%d') do
insert into dic_date(
date
,str_date
,local_date
,year
,month
,day
,dayofyear
,dayofmonth
,weekofyear
,weekofmonth
)select v_start_date date
,concat(v_start_date,'') str_date
,concat(year(v_start_date) ,'年'
,month(v_start_date),'月'
,day(v_start_date) ,'日') local_date
,year(v_start_date) year
,month(v_start_date) month
,day(v_start_date) day
,dayofyear(v_start_date) dayofyear
,dayofmonth(v_start_date) dayofmonth
,weekofyear(v_start_date) weekofyear
,week(v_start_date, 5)- week(date_sub(v_start_date, interval dayofmonth(v_start_date) - 1 day), 5) + 1 weekofmonth
from dual;
set v_start_date = date_add(v_start_date,interval 1 day) ;
end while;
end$$
delimiter ;
執行:
call pro_date_table();
mysql中建立維表 Mysql建立日期維表
說明日期維主要為與日期相關的維度,主要包括,周 月 季度 年等聚合維度的關聯計算.建立維表drop table if exists dim date create table dim date date key bigint comment primary key date id date comm...
mysql業務字典表 MySql常用字典表
1 顯示資料庫列表 mysql show databases 說明 其中字典庫是 information schema,其中常用字典表 information schema.schemata 資料庫中所有資料庫資訊 information schema.tables 存放資料庫中所有資料庫表資訊 i...
clickhouse建立外部字典表
本次使用mysql的資料,利用ck的dictionary 引擎建立外部字典表 將外部字典配置檔案拷貝到每台集群伺服器clickhouse的配置目錄下 etc clickhouse server 下,和config.xml同級,配置如下 以裝置字典為例device dictionary.xml dev...