Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL LOAD DATA INFILE ignore duplicate rows (autoincrement as primary key)

I ran into some trouble using LOAD DATA INFILE command as i wanted to ignore the lines that was already in the data base..say if i have a table with data as follows,

id  |name   |age -------------------- 1   |aaaa   |22 2   |bbbb   |21 3   |bbaa   |20 4   |abbb   |22 5   |aacc   |22 

Where id is auto increment value. an the csv file i have contains data as follows,

"cccc","14" "ssee","33" "dddd","22" "aaaa","22" "abbb","22" "dhgg","34" "aacc","22" 

I want to ignore the rows,

    "aaaa","22"     "abbb","22"     "aacc","22" 

and upload the rest to the table. and the query i have yet which uploads everything to the table is as follows,

    LOAD DATA INFILE 'member.csv'     INTO TABLE tbl_member     FIELDS TERMINATED BY ','            ENCLOSED BY '"'            ESCAPED BY '"'            LINES TERMINATED BY '\n'     (name, age); 

PLEASE help me on this task.. It will be much appreciated..i tried many links but did not help :(

like image 697
Hasitha Shan Avatar asked Oct 15 '12 07:10

Hasitha Shan


People also ask

Can primary key be repeated in MySQL?

When you insert a new row into a table if the row causes a duplicate in UNIQUE index or PRIMARY KEY , MySQL will issue an error. However, if you specify the ON DUPLICATE KEY UPDATE option in the INSERT statement, MySQL will update the existing row with the new values instead.

What is Local_infile MySQL?

The local_infile system variable controls server-side LOCAL capability. Depending on the local_infile setting, the server refuses or permits local data loading by clients that request local data loading. By default, local_infile is disabled. (This is a change from previous versions of MySQL.)

Which statement can you use to load data from a file into a table MySQL?

mysql> LOAD DATA LOCAL INFILE '/path/pet. txt' INTO TABLE pet; If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead: mysql> LOAD DATA LOCAL INFILE '/path/pet.

How do I change local Infile in MySQL?

Open my. ini file with a text editor. Look for the headers [client] , [mysql] under the CLIENT section & [mysqld] under the SERVER section. Save the file and restart MySQL80 service under the Windows local services.


2 Answers

Create a UNIQUE index on the age column, then:

LOAD DATA INFILE 'member.csv' IGNORE INTO TABLE tbl_member FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\n' (name, age); 
like image 198
Omar Avatar answered Sep 23 '22 16:09

Omar


One approach is to use a temporary table. Upload to this and use SQL to update tbl_member from temp table.

INSERT INTO tbl_member SELECT Field1,Field2,Field3,...  FROM temp_table WHERE NOT EXISTS(SELECT *               FROM tbl_member               WHERE (temp_table.Field1=tbl_member.Field1 and                    temp_table.Field2=tbl_member.Field2...etc.)             ) 
like image 29
david strachan Avatar answered Sep 24 '22 16:09

david strachan