97

I have a CSV file which contains 10 columns. I want to select only some columns from that file and load them into a MySQL database using the LOAD DATA INFILE command.

6 Answers 6

140

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I run your answer in mysql and appear an error: ERROR 1148 (42000): The used command is not allowed with this MySQL version.
What if i have 100 columns and I just want to import 2 columns, then should I write (@col1, @col2, ...@col100) set name=@col4, id-@col2; or there is an easy way?
@Dharma A python 3 program to print 100 column namesfor i in range(1,100): print("@column",i,",",end="",sep="")
Note: add "IGNORE 1 LINES" to ignore the headers
42
LOAD DATA INFILE 'file.csv'
  INTO TABLE t1
  (column1, @dummy, column2, @dummy, column3, ...)
FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '"'
LINES TERMINATED BY '\r\n';

Just replace the column1, column2, etc.. with your column names, and put @dummy anwhere there's a column in the CSV you want to ignore.

Full details here.

4 Comments

I get a syntax error when I run something like that. I have to put the (field,names) just before the semicolon at the end to get it to work.
This doesn't scale unless you know all the csv columns coming in.
how the F is this +38 votes ? It's wrong SQL syntax.
add "IGNORE 1 LINES" to ignore the headers
40

Specify the name of columns in the CSV in the load data infile statement.

The code is like this:

LOAD DATA INFILE '/path/filename.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
(column_name3, column_name5);

Here you go with adding data to only two columns(you can choose them with the name of the column) to the table.

The only thing you have to take care is that you have a CSV file(filename.csv) with two values per line(row). Otherwise please mention. I have a different solution.

Thank you.

7 Comments

probably the other answers are a bit outdated. this one worked for me with MySQL 5.6
Thank you for putting the full path to the file. For a moment, I was searching for the MySQL directory in which I have to put the csv file !
This one works the best although the path has to be from the allowed secure areas. Run this command SHOW VARIABLES LIKE "secure_file_priv"; and then drop the csv file in this location
Agree with @FabioNapodano. For my v5.7, I needed to put the column names at the end of the statement.
This doesn't answer the question, unfortunately. This requires there to be two columns in the .csv, and inserts them into two particular columns in the DB. The poster is requesting pulling 2 columns from a csv of 10.
|
20

Example:

contents of the ae.csv file:

"Date, xpto 14"
"code","number","year","C"
"blab","15885","2016","Y"
"aeea","15883","1982","E"
"xpto","15884","1986","B"
"jrgg","15885","1400","A"

CREATE TABLE Tabletmp (  
    rec VARCHAR(9) 
);

For put only column 3:

LOAD DATA INFILE '/local/ae.csv' 
INTO TABLE Tabletmp
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 2 LINES
(@col1, @col2, @col3, @col4, @col5)
set rec = @col3;


select * from Tabletmp;
    2016
    1982
    1986
    1400

3 Comments

Examples are always better
Beware of the line ending, use a program to see hidden characters, if the example does not give error but import 0 records, try with '\n'
If you are not on the server, that is, locally, you should are in the file folder and use the following argument: LOAD DATA local INFILE 'ae.csv'
2

if you have number of columns in your database table more than number of columns in your csv you can proceed like this:

LOAD DATA LOCAL INFILE 'pathOfFile.csv'
INTO TABLE youTable 
CHARACTER SET latin1 FIELDS TERMINATED BY ';' #you can use ',' if you have comma separated
OPTIONALLY ENCLOSED BY '"' 
ESCAPED BY '\\' 
LINES TERMINATED BY '\r\n'
(yourcolumn,yourcolumn2,yourcolumn3,yourcolumn4,...);

Comments

-1

For those who have the following error:

Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

You can simply run this command to see which folder can load files from:

SHOW VARIABLES LIKE "secure_file_priv";

After that, you have to copy the files in that folder and run the query with LOAD DATA LOCAL INFILE instead of LOAD DATA INFILE.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.