使用场景
A表中的a字段和B表中的x字段为关联 从B表中查询出y字段更新到A表中的b字段
解决方案
update A INNER JOIN (SELECT x,y from B) as temp on A.a = temp.x SET A.b = temp.y
测试案例
如图 A表的device_type_id 与 B表的id为关联 现在需要将A表中的device_type_code 更新为B表中对应的最新数据
运行sql
UPDATE
A
INNER JOIN (
SELECT
id,
device_type_code
FROM
B
)AS temp
ON A.device_type_id = temp.id
SET A.device_type_code = temp.device_type_code
WHERE A.id = 11698
查询结果
这样就可以按照需求更新数据了