Quantcast
Channel: How to do an update + join in PostgreSQL? - Stack Overflow
Viewing all articles
Browse latest Browse all 19

Answer by Mark Byers for How to do an update + join in PostgreSQL?

$
0
0

The UPDATE syntax is:

[ 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 ] [, ...] ]

In your case I think you want this:

UPDATE vehicles_vehicle AS v SET price = s.price_per_vehicleFROM shipments_shipment AS sWHERE v.shipment_id = s.id 

Or if you need to join on two or more tables:

UPDATE table_1 t1SET foo = 'new_value'FROM table_2 t2    JOIN table_3 t3 ON t3.id = t2.t3_idWHERE    t2.id = t1.t2_id    AND t3.bar = True;

Viewing all articles
Browse latest Browse all 19

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>