pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Craig Ringer <craig@postnewspapers.com.au>
Cc: pgsql-sql@postgresql.org
Subject: Re: Select into
Date: Thu, 20 Mar 2008 21:49:25 +0900
Message-ID: <47E25D55.9070908@postnewspapers.com.au> (raw)
In-Reply-To: <50DE8714-0EC7-432D-845B-4F8E6FE136DC@eclinic.com.au>
References: <94F93C0F-DA95-4435-B253-57C90961DF34@eclinic.com.au>
	<20080320104922.GB18935@a-kretschmer.de>
	<65937bea0803200358i290bdc87hdeca777b593b5df1@mail.gmail.com>
	<786D2B38-CE0A-41BF-9E70-4DEB22F8D9EF@eclinic.com.au>
	<47E252F8.6030008@postnewspapers.com.au>
	<50DE8714-0EC7-432D-845B-4F8E6FE136DC@eclinic.com.au>


>
>
>> craig=# update x set val = foundrow.val from ( select val from x 
>> where id = 2 ) as foundrow where id = 1 ;
>> UPDATE 1
>>
>
Thinking about it, it'd actually be better written as:

UPDATE x SET val = foundrow.val FROM ( SELECT val FROM x AS x2 WHERE 
x2.id = 2 ) AS foundrow WHERE id = 1;

... because it's nicer to use a table alias for x within the subquery 
and elimate any ambiguity for the reader about which "id" you're 
referring to. After all, it's also valid to reference the "id "field of 
the "x" outside the subquery within it, like in the following valid but 
rather nonsensical query:

UPDATE x SET val = (SELECT id+1) WHERE id = 1;

Using the table alias will not change the query plan at all, it just 
makes the reference to "id" within the subquery unambiguous to the reader.

Sorry for the repeat post.

--
Craig Ringer



view thread (43+ messages)  latest in thread

Message-ID: <47E25D55.9070908@postnewspapers.com.au>
Permalink:  ../47E25D55.9070908@postnewspapers.com.au/
Also on:    postgresql.org/message-id/47E25D55.9070908@postnewspapers.com.au

 · 

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: craig@postnewspapers.com.au
  Subject: Re: Select into
  In-Reply-To: <47E25D55.9070908@postnewspapers.com.au>

* 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