使用不同的日期格式将CSV导入MySQL [英] Importing a CSV to MySQL with different date format
问题描述
我正在做第一个CSV导入到MySQL,并注意到CSV中的日期格式 2011年1月31日
。如何转换为 2011-01-31
所以我可以把它放在DATE数据类型?第一件事是让PHP进行转换,然后把它插入第二个表,但我猜这是...不对。
I'm doing my first CSV import into MySQL and noticed that the date in the CSV has the format 31-Jan-2011
. How can I convert this to 2011-01-31
so I can place it in the DATE datatype? The first thing which came to mind is let PHP do the conversion then insert it into a 2nd table but I'm guessing that's...not right.
推荐答案
您可以在从CSV文件导入数据时替换格式,例如 -
You can replace format during importing data from the CSV file, for example -
LOAD DATA INFILE 'file_name.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(id, column2, column3, @date_time_variable) -- read one of the field to variable
SET date_time_column = STR_TO_DATE(@date_time_variable, '%d-%b-%Y'); -- format this date-time variable
它将格式化'31 -Jan-2011'到正确的DATETIME数据类型。
It will format the string like '31-Jan-2011' to a correct DATETIME data type.
此处的更多信息 - LOAD DATA INFILE语法。
More information here - LOAD DATA INFILE Syntax.
这篇关于使用不同的日期格式将CSV导入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!