根据postgresql查询结果更新表中数据

336 阅读1分钟

在开发过程中,我们会遇到需要根据某个sql的查询结果对表中满足条件的数据进行批量更新的操作,该贴就是用来记录此方法的。

update1 set 要更新的字段名 =2.from (sql查询结果) 表2 
where1和表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