0

i am using bash to load a file in mysql, and i have:

mysql --local-infile=1 -u user data_base_1 < file.sql

and file.sql is :

..$ cat file.sql
 load data local infile '/folder/load.csv' into table table_1 fields terminated by '|'

The code works fine.

The problem is that if the PK of one row in the file exist, the row is not inserted, and i need if the row exist insert and replace the row in the table. How can i do it?

Who can help me?

Thanks

2 Answers 2

1

You can specify REPLACE with LOAD DATA:

LOAD DATA LOCAL INFILE '/folder/load.csv' REPLACE INTO TABLE table_1 FIELDS TERMINATED BY '|'

Or else use the mysqlimport --replace option.

http://dev.mysql.com/doc/refman/5.6/en/mysqlimport.html#option_mysqlimport_replace

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

Comments

0

You could load into a temporary table and then execute two SQL statements:

UPDATE table
 WHERE ... (match found)
;
INSERT into table(...)
SELECT ...
  FROM temp_table
 WHERE NOT EXISTS(...)

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.