csv mysql 日期格式,使用不同的日期格式将CSV导入MySQL

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.

解决方案

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

It will format the string like '31-Jan-2011' to a correct DATETIME data type.

More information here - LOAD DATA INFILE Syntax.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值