How to skip columns in CSV file when importing into MySQL table using LOAD DATA INFILE?(使用 LOAD DATA INFILE 导入 MySQL 表时如何跳过 CSV 文件中的列?)
问题描述
我有一个有 11 列的 CSV 文件,我有一个有 9 列的 MySQL 表.
I've got a CSV file with 11 columns and I have a MySQL table with 9 columns.
CSV 文件如下所示:
The CSV file looks like:
col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11
MySQL 表如下所示:
and the MySQL table looks like:
col1, col2, col3, col4, col5, col6, col7, col8, col9
我需要将 CSV 文件的第 1-8 列直接映射到 MySQL 表的前 8 列.然后我需要跳过 CSV 文件中接下来的两列,然后将 CSV 文件的第 11 列映射到 MySQL 表的第 9 列.
I need to map the columns 1-8 of CSV file directly to the first 8 columns of the MySQL table. I then need to skip the next two columns in the CSV file and then map column 11 of CSV file to column 9 of MySQL table.
目前我正在使用以下 SQL 命令:
At the moment I am using the following SQL command:
LOAD DATA LOCAL INFILE 'filename.csv' INTO TABLE my_table
FIELDS TERMINATED BY ','
ENCLOSED BY ''
LINES TERMINATED BY '\n'
但是上面的代码将CSV文件的前9列映射到了MySQL表的9列.
But the above code maps the first 9 columns of CSV file to the 9 columns in the MySQL table.
推荐答案
来自 Mysql 文档:
您还可以通过以下方式丢弃输入值将其分配给用户变量和不将变量分配给表列:
You can also discard an input value by assigning it to a user variable and not assigning the variable to a table column:
LOAD DATA INFILE 'file.txt'
INTO TABLE t1 (column1, @dummy, column2, @dummy, column3);
这篇关于使用 LOAD DATA INFILE 导入 MySQL 表时如何跳过 CSV 文件中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 LOAD DATA INFILE 导入 MySQL 表时如何跳过 CSV 文件中的列?


基础教程推荐
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01