
Postgresql两表联合更新
近日使用Postgresql感到有点不好用,一个联合更新非要这样写语法才对:
update d_routetrip
set name=b.name ,
description=b.description
from d_scenery as b
where foreignid=b.id and d_routetrip.type='scenery'
如上所述,and 前的 d_routetrip表不能起别名,set后应用到此表也直接用字段表示,无需引用别名否则语法报错,而在and后引用的字段必须标明是属于哪个表的,否则也报错。好神经的用法
而普通的sql两表联合更新则简单多了,如下即可:
update Table1
set Table1.co1=Table2.co1,
Table1.co2=Table2.co2
from Table2
where Table2.id=Table1.id