Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import CSV into MySQL on duplicate key update

Tags:

sql

mysql

csv

I have a table looks like this.

create table test1 (
id1 varchar(10),
id2 varchar(10),
value1 varchar(10),
value2 varchar(10),
primary key (id1, id2)
);

which contains the data looks like this:

id1    id2    value1    value2
stan   marsh    1         2
eric   cartman  3         4

I have a local CSV file which contains the updated data (contains new and old) looks like this:

"stan", "marsh", "5", "6"
"kyle", "broflovski", "7", "8"

How to import the csv file an meanwhile, update on duplicate key.

Few Links that might be helpful

  1. Import and overwrite existing data in MySQL (Same question but it was not answered properly)
  2. Mysql Insert on duplicate
like image 200
B.Mr.W. Avatar asked Oct 25 '25 20:10

B.Mr.W.


1 Answers

Thanks a lot for the tiny flag advised by PM 77-1.

load data local infile '/Users/myuserID/Desktop/test1.csv' 
replace 
into table test.test1 
columns terminated by ',' 
enclosed by '"' 
lines terminated by '\n';
like image 54
B.Mr.W. Avatar answered Oct 28 '25 09:10

B.Mr.W.