hylian
hylian

Reputation: 560

MySQL SET UPDATE

I have two tables, let's call them Pets and Owners. Both tables have a column called country_id, for whatever reason somewhere along the way there are Pets.Country_ID that are not equal to Owners.Country_ID (they should match).

In order to fix this I've run the following:

UPDATE pets
INNER JOIN owners ON owners.id = pets.owner_id
SET pets.country_id = IF(owners.country_id != pets.country_id, 
pets.country_id, owner.country_id)
WHERE pets.country_id != owners.country_id

Is this correct? I have run the query but I'm still getting mismatched results.

Upvotes: 0

Views: 34

Answers (1)

juergen d
juergen d

Reputation: 204746

That is because your if clause, which is totally unnecessary, is wrong. Just remove it

UPDATE pets
INNER JOIN owners ON owners.id = pets.owner_id
SET pets.country_id = owners.country_id
WHERE pets.country_id != owners.country_id

Upvotes: 2

Related Questions