Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip columns in CSV file when importing into MySQL table using LOAD DATA INFILE?

I've got a CSV file with 11 columns and I have a MySQL table with 9 columns.

The CSV file looks like:

col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11 

and the MySQL table looks like:

col1, col2, col3, col4, col5, col6, col7, col8, col9 

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.

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' 

But the above code maps the first 9 columns of CSV file to the 9 columns in the MySQL table.

like image 603
Camsoft Avatar asked Jan 26 '10 11:01

Camsoft


People also ask

How do I insert selected columns from a CSV file to a MySQL database using load data infile?

Load data into a table in MySQL and specify columns: LOAD DATA LOCAL INFILE 'file. csv' INTO TABLE t1 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (@col1,@col2,@col3,@col4) set name=@col4,id=@col2 ; @col1,2,3,4 are variables to hold the csv file columns (assume 4 ) name,id are table columns.

How do I ignore a line in MySQL?

The IGNORE number LINES clause can be used to ignore lines at the start of the file. For example, you can use IGNORE 1 LINES to skip an initial header line containing column names: LOAD DATA INFILE '/tmp/test.

What does Columns terminated by clause in load data infile signify?

txt' INTO TABLE table2 FIELDS TERMINATED BY '\t'; The likely result is that each input line would be interpreted as a single field. LOAD DATA INFILE can be used to read files obtained from external sources, too. For example, a file in dBASE format will have fields separated by commas and enclosed in double quotes.

Which is more efficient load data insert?

LOAD DATA (all forms) is more efficient than INSERT because it loads rows in bulk. The server must parse and interpret only one statement, not several.


1 Answers

From Mysql docs:

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); 
like image 175
grapefrukt Avatar answered Sep 23 '22 02:09

grapefrukt