首页 > 代码库 > Mysql时间维度表
Mysql时间维度表
1、建一张表test_date用来日期的自增长,里面只放一列 ID,写一个存储,插入顺序数字
BEGIN #Routine body goes here... declare id int; set id=1; while id<=6000 do insert into test_date values(id); set id=id+1; end while; END
2. 建立时间维度表,插入记录
SET @d0 = "2000-01-01"; SET @d1 = "2020-12-31"; SET @str = "Q"; SET @date = date_sub(@d0, interval 1 day); INSERT INTO dim_time SELECT @date := date_add(@date, interval 1 day), year(@date), month(@date), DAYOFYEAR(@date), day(@date) as d, weekday(@date)+1 as wd, week(@date, 3) as w, dayname(@date) as wd_name, monthname(@date) as m_name, quarter(@date) as q, CONCAT(@str,quarter(@date)) as qq, DAYOFMONTH(last_day(@date)), date_format(@date, "%Y%m%d"), date_format(@date, ‘%d-%m-%Y‘)as yw FROM test_date WHERE date_add(@date, interval 1 day) <= @d1 ORDER BY id;
Mysql时间维度表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。