来自csv的LOAD DATA LOCAL INFILE日期格式

时间:2015-03-13 21:18:23

标签: php mysql load-data-infile

我一直在尝试将文件拖入表中,而我唯一的问题是导入日期(目前采用'MM/DD/YY'格式)。无论我尝试什么,他们最终看起来像'0008-12-14'。该日期在.csv文件中为8/12/14。我试过了

SET ShipDate = STR_TO_DATE(@ShipDate,'%Y%m%d')

但随后将其导入为NULL。表字段设置为DATE。这是代码:

$query = <<<eof
    LOAD DATA LOCAL INFILE 'file.csv'
     INTO TABLE HS_import
     FIELDS TERMINATED BY ','
     LINES TERMINATED BY '\n'
     IGNORE 1 LINES
    (AmountPaid,
ShipDate)
SET ShipDate = STR_TO_DATE(@ShipDate,'%Y%m%d')
eof;

1 个答案:

答案 0 :(得分:0)

STR_TO_DATE的Yor格式错误。它应指定INPUT格式而不是OUTPUT格式:

SET ShipDate = STR_TO_DATE(@ShipDate,'%d%m%y')

所以整个事情看起来应该是

  LOAD DATA LOCAL INFILE 'file.csv'
  INTO TABLE HS_import
  FIELDS TERMINATED BY ','
  LINES TERMINATED BY '\n'
  IGNORE 1 LINES
  (AmountPaid, @ShipDate)
  SET ShipDate = STR_TO_DATE(@ShipDate,'%d%m%y')