user2797028
user2797028

Reputation: 31

MySQL update table using import data from Text file

LOAD DATA LOCAL INFILE 'D:\\dummy data.txt' INTO TABLE mmc_avado.avado_mmc_file FIELDS TERMINATED BY '|'
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

I have this query to import date from text file to mysql. But I want....

  1. If data in mysql and text file are same then do not import that line.
  2. If data in text file has some updated fields which is not in mysql for the same row. Like both has that row but text file has some updated fields.

Then how we perform this. Please help.

Upvotes: 2

Views: 3656

Answers (1)

Quassnoi
Quassnoi

Reputation: 425823

You should import the data into a staging (possibly temporary) table then add the new data into the target table like this:

INSERT
INTO    mmc_avado.avado_mmc_file
SELECT  *
FROM    staging_table
ON DUPLICATE KEY
UPDATE  col1 = VALUES(col1),
        col2 = VALUES(col2),
        ...

Upvotes: 1

Related Questions