MySQL 设置更新

标签 mysql

我有两张 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/

相关文章:

php - 如何在php中恢复备份的mysql文件

mysql - 从特定字段中查找 MySQL 中已定义字符串的最大值(以逗号分隔)

PHP Mysql 数组之间

php - 这个 PHP PDO 例程有什么问题?

python - python向mysql插入中文字符时出现UnicodeEncodeError

mysql - SQL,为什么显式查找数据比 SELECT * 更快

php - Mysql中如何处理带有内嵌变量的字符串

mysql - 我需要将新项目添加到 SQL 表中并更新旧项目

MySQL concat 与 INNER JOIN

php - 在数据库中保留 1 周数据