使用不同的日期格式将 CSV 导入 MySQL [英] Importing a CSV to MySQL with different date format

查看:45
本文介绍了使用不同的日期格式将 CSV 导入 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的第一个 CSV 导入 MySQL 并注意到 CSV 中的日期格式为 31-Jan-2011.如何将其转换为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆