LOAD DATA LOCAL INFILE处理某些日期列

时间:2019-06-28 21:06:06

标签: mysql csv

我有一个包含3个日期列的csv,但每个列的格式均为dd / mm / yyyy H:i:s,即27/05/2019 20:25:00

我试图操纵这些文件以使用LOAD DATA INFILE插入但没有成功:

我的状态如下:

 LOAD DATA LOCAL INFILE '/file.csv' 
    INTO TABLE db_table FIELDS TERMINATED BY ',' 
    OPTIONALLY ENCLOSED BY '"' 
    LINES TERMINATED BY '\n' 
    IGNORE 1 LINES
    SET promotion_starts = str_to_date(@column7, '%d/%m/%Y %H:%i:%s'), 
    promotion_ends = str_to_date(@column8, '%d/%m/%Y %H:%i:%s'), 
    date_added = str_to_date(@column17, '%d/%m/%Y %H:%:%i:%s')

所有其他数据都可以插入,但日期列都为空

1 个答案:

答案 0 :(得分:2)

您缺少查询的行,该行指定CSV文件的字段与表列的对应方式,并定义了@column7@column8

 LOAD DATA LOCAL INFILE '/file.csv' 
    INTO TABLE db_table FIELDS TERMINATED BY ',' 
    OPTIONALLY ENCLOSED BY '"' 
    LINES TERMINATED BY '\n' 
    IGNORE 1 LINES
    (foo, bar, baz, xxx, yyy, zzz, @column7, @column8, aaa, bbb, ccc, ddd, eee, fff, ggg, hhh, @column17, iii, jjj)
    SET promotion_starts = str_to_date(@column7, '%d/%m/%Y %H:%i:%s'), 
    promotion_ends = str_to_date(@column8, '%d/%m/%Y %H:%i:%s'), 
    date_added = str_to_date(@column17, '%d/%m/%Y %H:%:%i:%s')

将所有由我组成的列名替换为表中与CSV字段相对应的实际列名。