如何做一个更新加入PostgreSQL?

前端之家收集整理的这篇文章主要介绍了如何做一个更新加入PostgreSQL?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
基本上,我想这样做:
update vehicles_vehicle v 
    join shipments_shipment s on v.shipment_id=s.id 
set v.price=s.price_per_vehicle;

我相信这将工作在MysqL(我的背景),但它似乎不工作在postgres。我得到的错误是:

ERROR:  Syntax error at or near "join"
LINE 1: update vehicles_vehicle v join shipments_shipment s on v.shi...
                                  ^

当然有一个简单的方法来做到这一点,但我找不到正确的语法。那么,我该如何在Postgresql中写这个?

UPDATE syntax是:
[ WITH [ RECURSIVE ] with_query [,...] ]
UPDATE [ ONLY ] table [ [ AS ] alias ]
    SET { column = { expression | DEFAULT } |
          ( column [,...] ) = ( { expression | DEFAULT } [,...] ) } [,...]
    [ FROM from_list ]
    [ WHERE condition | WHERE CURRENT OF cursor_name ]
    [ RETURNING * | output_expression [ [ AS ] output_name ] [,...] ]

在你的情况下,我想你想要:

UPDATE vehicles_vehicle AS v 
SET price = s.price_per_vehicle
FROM shipments_shipment AS s
WHERE v.shipment_id = s.id

猜你在找的Postgre SQL相关文章