Jie
Jie

Reputation: 177

How to check duplicate and upload the new data when I use PHP to upload the CSV file into MySQL database

I have a CSV file which have ten column:

ID, fromDate, endDate, address, price1, price2, column1, column2, column3, column4

but I just want to insert the first six columns into the MySQL database, and most important, if there exist a tuple in the database that the attribute(ID, fromDate, endDate, address) are the same, We defined that the two rows are identify.

for example: there exist a tuple in the database:

(1000, 2014-11-21, 2014-11-23, new york, 100, 200)

and Now, in the CSV file, there exists a row:

(1000, 2014-11-21, 2014-11-23, new york, 150, 250)

We defined that the two tuple are the same. So now we should update the data in the database by the new data in the CSV file.

which means, delete the tuple

(1000, 2014-11-21, 2014-11-23, new york, 100, 200)

and insert the new tuple:

(1000, 2014-11-21, 2014-11-23, new york, 150, 250)

How to do this?

how to check for the duplicate when insert the data from CSV into database, and once found a duplicate row, how to replace it?

Upvotes: 0

Views: 1045

Answers (1)

sectus
sectus

Reputation: 15464

You could use specific mysql Replace

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

Or Insert ON duplicate, but with this approach you have to list all your fields.

Upvotes: 1

Related Questions