Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 8B9984761D8 for ; Wed, 1 May 2002 13:38:27 -0400 (EDT) Received: from joeconway.com (unknown [63.210.180.150]) by postgresql.org (Postfix) with ESMTP id C1D07475F82 for ; Wed, 1 May 2002 13:09:04 -0400 (EDT) Received: from [192.168.5.2] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 3.5.6) with ESMTP-TLS id 357756; Wed, 01 May 2002 09:02:54 -0700 Message-ID: <3CD02137.2080708@joeconway.com> Date: Wed, 01 May 2002 10:09:11 -0700 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.9) Gecko/20020311 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Gary Stainburn Cc: pgsql-sql@postgresql.org Subject: Re: problem with update from subselect References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200205/5 X-Sequence-Number: 7641 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-01 11: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