简体   繁体   English

将csv文件数据插入包含更多列的mysql表中

[英]Insert csv file data into mysql table which contains more columns

I want to add the data from a csv file to a table in the DB but the table contains more columns than the file. 我想将csv文件中的数据添加到数据库中的表中,但是该表包含的列比文件更多。 The query should be such that some of the columns are filled by the csv file and some others should be filled with a specific value in the query. 该查询应使csv文件填充某些列,而在查询中应填充特定的值。 Lets say my csv file have data written in the format: "value1,value2" in every line. 可以说,我的csv文件中的数据每行都以“ value1,value2”格式编写。 I want to add the data in a table containing 3 columns. 我想将数据添加到包含3列的表中。 First two columns should be filled with data from the file and in the third column should be the value "1". 前两列应填充文件中的数据,第三列应为值“ 1”。 So far I have the following query which only fills the 2 columns of the table with data from the csv file: 到目前为止,我有以下查询,该查询仅使用来自csv文件的数据填充表的2列:

LOAD DATA LOCAL INFILE 'target_file'
INTO TABLE table_name FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n' (col1,col2);

You can mark default value "1" for the third field and then import the csv file. 您可以将第三个字段的默认值标记为“ 1”,然后导入csv文件。 Or you can mark the fields as nullable that are not included in the csv file, then execute a query to update the null values. 或者,您可以将csv文件中未包含的字段标记为可空字段,然后执行查询以更新空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM