Robert Kaiser
Robert Kaiser

Reputation: 21

MySQL Update only changes

I've imported a file to a MySQL table and now I want to update an existing table only with the changes.

I'm trying this with a select first before doing it as an update.

Both tables have an unique id field and value fields.

The following query doesn't give any results although there are differences in the value field:

SELECT a.id, a.value, b.value FROM a, b WHERE a.id=b.id AND a.value!=b.value

When I try it with a.value=b.value it works, but I need the changes.

Any Ideas?

My goal is to update table a in the end with changed values from table b but I can't update everything because I don't want to change a "lastedit" field if there are no changes.

I can't change the structure of table a to an automatic "lastedit" field.

Upvotes: 2

Views: 2689

Answers (1)

Uueerdo
Uueerdo

Reputation: 15971

UPDATE a INNER JOIN b ON a.id = b.id
SET a.value = b.value 
WHERE a.value <> b.value
;

But of course, this only updates anything if (as Marc B said in his comment above) the tables have corresponding id values.

Upvotes: 2

Related Questions