pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Gary Stainburn <gary.stainburn@ringways.co.uk>
Cc: pgsql-sql@postgresql.org
Subject: Re: problem with update from subselect
Date: Wed, 01 May 2002 10:09:11 -0700
Message-ID: <3CD02137.2080708@joeconway.com> (raw)
References: <E172wxt-0007vR-00@stan.ringways.co.uk>

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




view thread (3+ messages)  latest in thread

Message-ID: <3CD02137.2080708@joeconway.com>
Permalink:  ../3CD02137.2080708@joeconway.com/
Also on:    postgresql.org/message-id/3CD02137.2080708@joeconway.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mail@joeconway.com, gary.stainburn@ringways.co.uk
  Subject: Re: problem with update from subselect
  In-Reply-To: <3CD02137.2080708@joeconway.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox