首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只在MYSQL的单个列中存储月份和日期

问只在MYSQL的单个列中存储月份和日期
EN

Stack Overflow用户
提问于 2017-12-08 10:48:24
回答 2查看 41关注 0票数 0

嗨,我试着把月份和日期存储在一个单一的列中。

UseCase

我把数据转储到mysql中是为了报告的目的,并且是每月报告。

稍后我将使用Hibernate来恢复数据。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-08 11:45:26

对于您希望使用的格式,我还不完全清楚,但假设将月份存储为一个数字,后面跟着日期,并由某种分隔符分隔,则可以这样做。只是第二个INSERT语句实际上执行您询问的存储。

尽管如此,如果您有一个合适的格式的原始数据,我建议您使用一个单一的datetime列进行存储,并在必要时将其拆分为月份和日期,以便稍后恢复使用它。

代码语言:javascript
复制
-- make a table to hold some dates
CREATE TABLE testdates ( thedate DATE DEFAULT NULL) ;

-- make a table to hold just the month and date in one column as requested
CREATE TABLE tbl_month_and_date (the_month_and_date VARCHAR(14) DEFAULT NULL ); 

-- put some dummy dates into the testdates table
INSERT INTO testdates (thedate)
VALUES
('2017-09-14'),
('2017-10-13'),
('2017-11-27'),
('2017-08-20');


-- insert the month and date into a single column in the tbl_month_and_date table
-- in this case separated my <space> <colon> <space>
INSERT INTO tbl_month_and_date (the_month_and_date)
SELECT CONCAT(MONTH(thedate),' : ', thedate)
FROM testdates;

    -- show what is now in tbl_month_and_date
SELECT the_month_and_date
FROM tbl_month_and_date

表tbl_month_and_date的内容现在

代码语言:javascript
复制
the_month_and_date  
9 : 2017-09-14      
10 : 2017-10-1      
11 : 2017-11-2      
8 : 2017-08-20  
票数 0
EN

Stack Overflow用户

发布于 2017-12-08 11:49:43

最好是将时间戳用于替换目的,而不是只使用日期、月份或两者兼有。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47712779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档