在开发过程中,我们会遇到需要根据某个sql的查询结果对表中满足条件的数据进行批量更新的操作,该贴就是用来记录此方法的。
update 表1 set 要更新的字段名 = 表2.值
from (sql查询结果) 表2
where 表1和表2的关联
案例
update t_report a set unit = b."name"
from (select t1."name",t2.project_id from t_company t1 inner join t_project t2 on t1.uuid = t2.company) b
where a.project_id = b.project_id