Re: Select into - Mailing list pgsql-sql

From Craig Ringer
Subject Re: Select into
Date
Msg-id 47E25D55.9070908@postnewspapers.com.au
Whole thread Raw
In response to Re: Select into  (Gavin 'Beau' Baumanis <gavinb@eclinic.com.au>)
List 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


pgsql-sql by date:

Previous
From: Gavin 'Beau' Baumanis
Date:
Subject: Re: Select into
Next
From: Joe
Date:
Subject: Re: Select into