4rk
4rk

Reputation: 375

Insert and update from table in tmp database to table in another database

I am currently trying to load the data from a csv file to a table in a database, insert any new entries and update old entries. The table has columns say (field1, field2, field3, field4, field5) with field1 as the primary key and the csv has values with only the columns (field1, field2, field3). What would be the best way to approach this? Currently I was thinking of approaching it as follows:

Any comments on improving/optimizing/correcting the steps?

Upvotes: 1

Views: 465

Answers (1)

Since you are using MySQL I suggest this non-general solution.

Include field4 and field5 in your tmp table.

After creating the tmp table update field4 and field5 with values for existing records in table1.

UPDATE tmp.tmp_table s INNER JOIN db.table1 t ON (t.field1=s.field1)
SET s.field4 = t.field4, s.field5 =t.field5

Then

REPLACE INTO db.table1 VALUES (field1, field2, field3, field4, field5)
SELECT t.field1, t.field2, t.field3, t.field4, t.field5 
FROM tmp.tmp_table t

Upvotes: 1

Related Questions