Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 644302E0049 for ; Thu, 20 Mar 2008 09:49:41 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 36991-05 for ; Thu, 20 Mar 2008 09:49:39 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [202.61.230.242]) by postgresql.org (Postfix) with ESMTP id F326C2E005A for ; Thu, 20 Mar 2008 09:49:38 -0300 (ADT) Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by www.postnewspapers.com.au (Postfix) with ESMTP id 095125B7 for ; Thu, 20 Mar 2008 21:49:34 +0900 (WST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id AFD5A1E0436 for ; Thu, 20 Mar 2008 21:49:34 +0900 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 8MPp7s8ifKZa for ; Thu, 20 Mar 2008 21:49:32 +0900 (WST) Received: from [203.161.97.213] (203.161.97.213.static.amnet.net.au [203.161.97.213]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTP id 3D3211E0434 for ; Thu, 20 Mar 2008 21:49:32 +0900 (WST) Message-ID: <47E25D55.9070908@postnewspapers.com.au> Date: Thu, 20 Mar 2008 21:49:25 +0900 From: Craig Ringer User-Agent: Thunderbird 2.0.0.12 (Windows/20080213) MIME-Version: 1.0 Cc: pgsql-sql@postgresql.org Subject: Re: Select into 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> In-Reply-To: <50DE8714-0EC7-432D-845B-4F8E6FE136DC@eclinic.com.au> X-Enigmail-Version: 0.95.6 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.101 tagged_above=0 required=5 tests=AWL=-1.480, MISSING_HEADERS=1.581 X-Spam-Level: X-Archive-Number: 200803/141 X-Sequence-Number: 30469 > > >> 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