Re: Select into

From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To:
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Select into
Date: 2008-03-20 12:49:25
Message-ID: 47E25D55.9070908@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


>
>
>> 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Gurjeet Singh 2008-03-20 13:13:47 Re: Select into
Previous Message Gavin 'Beau' Baumanis 2008-03-20 12:20:27 Re: Select into