Reputation: 2553
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. 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. Lets say my csv file have data written in the format: "value1,value2" in every line. I want to add the data in a table containing 3 columns. First two columns should be filled with data from the file and in the third column should be the value "1". So far I have the following query which only fills the 2 columns of the table with data from the csv file:
LOAD DATA LOCAL INFILE 'target_file'
INTO TABLE table_name FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n' (col1,col2);
Upvotes: 0
Views: 722
Reputation: 1351
You can mark default value "1" for the third field and then import the csv file. 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.
Upvotes: 1