【发布时间】:2016-07-12 08:09:44
【问题描述】:
我正在尝试使用以下字段创建一个 mysql 表: 年、月、日、时、分
从 10 年前到未来 10 年。
我有这个可以创建几天,但它不包括小时和分钟
SET @d0 = "2012-01-01";
SET @d1 = "2012-12-31";
SET @date = date_sub(@d0, interval 1 day);
# set up the time dimension table
DROP TABLE IF EXISTS time_dimension;
CREATE TABLE `time_dimension` (
`date` date DEFAULT NULL,
`id` int NOT NULL,
`y` smallint DEFAULT NULL,
`m` smallint DEFAULT NULL,
`d` smallint DEFAULT NULL,
`yw` smallint DEFAULT NULL,
`w` smallint DEFAULT NULL,
`q` smallint DEFAULT NULL,
`wd` smallint DEFAULT NULL,
`m_name` char(10) DEFAULT NULL,
`wd_name` char(10) DEFAULT NULL,
PRIMARY KEY (`id`)
);
# populate the table with dates
INSERT INTO time_dimension
SELECT @date := date_add(@date, interval 1 day) as date,
# integer ID that allows immediate understanding
date_format(@date, "%Y%m%d") as id,
year(@date) as y,
month(@date) as m,
day(@date) as d,
date_format(@date, "%x") as yw,
week(@date, 3) as w,
quarter(@date) as q,
weekday(@date)+1 as wd,
monthname(@date) as m_name,
dayname(@date) as wd_name
FROM T
WHERE date_add(@date, interval 1 day) <= @d1
ORDER BY date
;
我怎样才能修改它,使它也能做几小时和几分钟? 谢谢!
【问题讨论】:
-
您有什么理由不想为此使用时间戳吗?我认为管理这么多不同的时间组件有时会变得一团糟。
-
但是为什么?哦,人性:-(
-
请告诉我们您为什么要这样做?存储 20 年内每分钟的时间记录的表的目的是什么?
-
我想存储 17k 个股票市场符号的历史数据,我认为它不仅可以防止与数据提供者通信时出现不一致(因为时间维度表会有一个防止重复的索引),而且检索系列也会更快,因为我不必根据时间戳即时分组,它会在时间维度上抛出索引
-
我不明白您为什么要这样做,但是,您可以查看有关 date_add() 函数的 MySQL 文档并检查 MySQL 中是否有其他函数可以检索 datetime 值的其他部分(是的,有)。基于这些,您可以更新您的查询。
标签: php mysql data-warehouse