首页 > 解决方案 > MySQL 设置更新

问题描述

我有两张桌子,我们称它们为 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

这个对吗?我已经运行了查询,但我仍然得到不匹配的结果。

标签: mysql

解决方案


那是因为您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

推荐阅读