Re: problem with update from subselect - Mailing list pgsql-sql

From Joe Conway
Subject Re: problem with update from subselect
Date
Msg-id 3CD02137.2080708@joeconway.com
Whole thread Raw
In response to problem with update from subselect  (Gary Stainburn <gary.stainburn@ringways.co.uk>)
List pgsql-sql
Gary Stainburn wrote:
> Hi all, using the schema described below, I want to be able to update each 
> arrival time from departure times and trip lengths.
> 
> However the update fails because the subselect returns all three answers.
> 
> How would I correct the update to make it work
> 
> update trip set trip_arrive = (select t.trip_depart + r.route_time
>   from route r, trip t where r.routeid = t.trip_route);
> 

If I understand what you're trying to do correctly, this works:

test=# update trip set trip_arrive = trip_depart + r.route_time from 
route r where r.routeid = trip.trip_route;              UPDATE 3
test=# select * from trip; tripid | trip_route |     trip_depart     |     trip_arrive
--------+------------+---------------------+---------------------      1 |          1 | 2002-01-01 10:00:00 |
2002-01-0111:40:00      2 |          1 | 2002-02-01 11:30:00 | 2002-02-01 13:10:00      3 |          2 | 2002-01-01
11:00:00| 2002-01-01 11:30:00
 
(3 rows)


HTH,
Joe



pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: problem with update from subselect
Next
From: j.konzack@arcor.de
Date:
Subject: CREATE VIEW question...