我有一张 table users
使用现有数据,并将更新后的数据上传到临时表 users_temp
.
我正在尝试创建两个查询,一个用于更新 name
和 department_id
users
中的行数据来自 users_temp
关于匹配id
.
另一个删除 users
中的行没有匹配 id
在users_temp
.
请帮忙。
最佳答案
您可以通过在您的更新中添加一个连接来做到这一点。
UPDATE users u
INNER JOIN users_temp ut on u.id = ut.id //or whatever the matching fields are
SET u.name = ut.name, u.department_id = ut.department_id;
我敢肯定有人会为第二个查询提供更有效的示例,但这可以解决问题:
关于MySQL:从另一个表中具有匹配键的行更新表中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33357446/