导入具有多种自定义日期格式的 csv mysql
Import csv mysql with multiple custom date formats
我必须将 csv 导入到具有有趣日期格式的 mysql 数据库中,所以我决定使用 STR_TO_Date() 来转换它,但是它在第二次转换时出现错误我尝试对日期进行多次转换。
LOAD DATA LOCAL INFILE 'e:/the.csv'
INTO TABLE data
FIELDS OPTIONALLY ENCLOSED BY '"' TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(ID, UID, Barcode, Name, @Start, @End, Seconds)
SET Start = STR_TO_DATE(@Start, '%d/%c/%Y %H:%i')
SET End = STR_TO_DATE(@End, '%d/%c/%Y %H:%i')
我的数据库看起来是这样的:
ID UID Barcode Name Start End Seconds
29 12345 BBB6168 namehere 09/12/2014 18:00 09/12/2014 18:09 538
30 12345 AAA6168 namehere 10/12/2014 11:01 10/12/2014 11:02 4
31 AAAAA ABC6084 namehere 10/12/2014 11:34 10/12/2014 11:34 5
我该如何解决这个问题?
正如 Barranka 所提到的,最好导入 table 字符串 (varchar) 列,然后 运行 插入到最终的 table.
LOAD DATA LOCAL INFILE 'e:/the.csv'
INTO TABLE data_temp_strings
FIELDS OPTIONALLY ENCLOSED BY '"' TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(ID, UID, Barcode, Name, StartStr, EndStr, Seconds);
INSERT INTO data (ID,UID,Barcode,Name,Start,End,Seconds)
SELECT ID,
UID,
Barcode,
Name,
STR_TO_DATE(StartStr, '%d/%c/%Y %H:%i'),
STR_TO_DATE(EndStr, '%d/%c/%Y %H:%i'),
Seconds
FROM data_temp_strings;
根据MySQL documentation for LOAD DATA INFILE,代码如下:
LOAD DATA LOCAL INFILE 'e:/the.csv'
INTO TABLE data
FIELDS OPTIONALLY ENCLOSED BY '"' TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(ID, UID, Barcode, Name, @Start, @End, Seconds)
SET Start = STR_TO_DATE(@Start, '%d/%c/%Y %H:%i'),
End = STR_TO_DATE(@End, '%d/%c/%Y %H:%i');
我必须将 csv 导入到具有有趣日期格式的 mysql 数据库中,所以我决定使用 STR_TO_Date() 来转换它,但是它在第二次转换时出现错误我尝试对日期进行多次转换。
LOAD DATA LOCAL INFILE 'e:/the.csv'
INTO TABLE data
FIELDS OPTIONALLY ENCLOSED BY '"' TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(ID, UID, Barcode, Name, @Start, @End, Seconds)
SET Start = STR_TO_DATE(@Start, '%d/%c/%Y %H:%i')
SET End = STR_TO_DATE(@End, '%d/%c/%Y %H:%i')
我的数据库看起来是这样的:
ID UID Barcode Name Start End Seconds
29 12345 BBB6168 namehere 09/12/2014 18:00 09/12/2014 18:09 538
30 12345 AAA6168 namehere 10/12/2014 11:01 10/12/2014 11:02 4
31 AAAAA ABC6084 namehere 10/12/2014 11:34 10/12/2014 11:34 5
我该如何解决这个问题?
正如 Barranka 所提到的,最好导入 table 字符串 (varchar) 列,然后 运行 插入到最终的 table.
LOAD DATA LOCAL INFILE 'e:/the.csv'
INTO TABLE data_temp_strings
FIELDS OPTIONALLY ENCLOSED BY '"' TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(ID, UID, Barcode, Name, StartStr, EndStr, Seconds);
INSERT INTO data (ID,UID,Barcode,Name,Start,End,Seconds)
SELECT ID,
UID,
Barcode,
Name,
STR_TO_DATE(StartStr, '%d/%c/%Y %H:%i'),
STR_TO_DATE(EndStr, '%d/%c/%Y %H:%i'),
Seconds
FROM data_temp_strings;
根据MySQL documentation for LOAD DATA INFILE,代码如下:
LOAD DATA LOCAL INFILE 'e:/the.csv'
INTO TABLE data
FIELDS OPTIONALLY ENCLOSED BY '"' TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(ID, UID, Barcode, Name, @Start, @End, Seconds)
SET Start = STR_TO_DATE(@Start, '%d/%c/%Y %H:%i'),
End = STR_TO_DATE(@End, '%d/%c/%Y %H:%i');