Importing a CSV to MySQL with different date format(以不同的日期格式将 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 '
'
(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
它将像2011 年 1 月 31 日"这样的字符串格式化为正确的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以不同的日期格式将 CSV 导入 MySQL
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
