0

Let me try to make it as simple as possible.

We have 3 columns here: ID | Title | Content

I have 2 CSV files and I import file 1 with this code:

LOAD DATA INFILE 'file_1.csv' IGNORE 
INTO TABLE scrapped 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

It imports successfully. Now I want to import file 2, but I am aware that some of the file 2 has the same "Title" as some of the file 1, but different "ID" and "Content".

How do I make it so, if the file 2 finds the same "Title", instead of inserting it will replace the "Title" with its own "ID" and "Content"?

Dharman
  • 30,962
  • 25
  • 85
  • 135
R Records
  • 17
  • 4
  • Can you please tell me what field1 and field2 are in the code of the solution? – R Records Jan 02 '19 at 13:12
  • Load data infile allows you to specify the fields/columns you wish to load to. Please review https://dev.mysql.com/doc/refman/8.0/en/load-data.html – P.Salmon Jan 02 '19 at 13:32
  • INSERT INTO X SELECT * FROM temporary_table ON DUPLICATE KEY UPDATE `content` = VALUES(content); – R Records Jan 02 '19 at 13:46
  • It doesn't do anything after that line. Content is still the same as before. My PRIMARY is the Title. – R Records Jan 02 '19 at 13:46

1 Answers1

0

If you don't have unique columns it should import all data - you use "IGNORE" in your statement.

How do I make it so, if the file 2 finds the same "Title", instead of inserting it will replace the "Title" with its own "ID" and "Content"?

In this case you should use "REPLACE" instead of "IGNORE" like this:

LOAD DATA INFILE 'file_1.csv' REPLACE

Ricky Lee
  • 96
  • 3
  • But it's not always it should replace, because file 2 still has new data to be INSERT-ed. – R Records Jan 02 '19 at 13:32
  • Then you should be more specific when asking a question. Personally, I use additional scripts (e.g. python, php) while working with CSV, which helps with more complex tasks. – Ricky Lee Jan 02 '19 at 14:07