我有两张 table ,我们称它们为 Pets 和 Owners。两个表都有一个名为“country_id”的列,无论出于何种原因,其中的某个地方有 Pets.Country_ID 不等于 Owners.Country_ID(它们应该匹配)。
为了解决这个问题,我运行了以下命令:
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
这是正确的吗?我已经运行了查询,但仍然得到不匹配的结果。
最佳答案
那是因为你的 if
子句是错误的,这是完全没有必要的。删除即可
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
关于MySQL 设置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50697212/