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

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

$
0
0

In case you don't have the value in one column but instead had to calculate it from the other table (in this example price_per_vehicle from shipments_shipment).Then assuming that shipments_shipment has price and vehicle_id columns the update for a specific vehicle could look like this:

-- Specific vehicle in this example is with id = 5WITH prices AS (    SELECT SUM(COALESCE(s.price, 0)) AS price_per_vehicle    FROM shipments_shipment AS s    WHERE s.vehicle_id = 5)UPDATE vehicles_vehicle AS vSET v.price = prices.price_per_vehicleFROM pricesWHERE v.id = 5

Viewing all articles
Browse latest Browse all 19

Trending Articles



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