嗨,我试着把月份和日期存储在一个单一的列中。
UseCase
我把数据转储到mysql中是为了报告的目的,并且是每月报告。
稍后我将使用Hibernate来恢复数据。
发布于 2017-12-08 11:45:26
对于您希望使用的格式,我还不完全清楚,但假设将月份存储为一个数字,后面跟着日期,并由某种分隔符分隔,则可以这样做。只是第二个INSERT语句实际上执行您询问的存储。
尽管如此,如果您有一个合适的格式的原始数据,我建议您使用一个单一的datetime列进行存储,并在必要时将其拆分为月份和日期,以便稍后恢复使用它。
-- 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的内容现在
the_month_and_date
9 : 2017-09-14
10 : 2017-10-1
11 : 2017-11-2
8 : 2017-08-20 发布于 2017-12-08 11:49:43
最好是将时间戳用于替换目的,而不是只使用日期、月份或两者兼有。
https://stackoverflow.com/questions/47712779
复制相似问题