Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
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

pgsql-sql by date

Next:From: Gurjeet SinghDate: 2008-03-20 13:13:47
Subject: Re: Select into
Previous:From: Gavin 'Beau' BaumanisDate: 2008-03-20 12:20:27
Subject: Re: Select into

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group