Richard
Richard

Reputation: 318

Merge two tables together

We had a problem where somebody accidentally over-wrote a certain field in a table for specific customers, so we've restored a backup to a separate database.

I'm trying to write a query which will take the one field from the backup version, and overwrite the live version with it.

I'm thinking:

UPDATE live.orders
SET live.orders.price = backup.orders.price
WHERE live.orders.id = backup.orders.id
AND live.orders.date > '2017-01-01 00:00:00'

Would this have the desired effect of replacing the price field in the live database with what's been restored from the backup for all orders places this year?

Also, is there anyway to test this and view the output before it actually changes the table?

Thanks

Upvotes: 0

Views: 47

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

Seems you need an update based on join

  UPDATE live.orders
  JOIN backup.orders ON live.orders.id = backup.orders.id
  SET live.orders.price = backup.orders.price
  WHERE live.orders.date > '2017-01-01 00:00:00'

Upvotes: 1

Related Questions