X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 8D1E1D1B258 for ; Tue, 29 Jun 2004 13:17:53 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 66727-02 for ; Tue, 29 Jun 2004 16:17:44 +0000 (GMT) Received: from dyatel.antar.bryansk.ru (unknown [195.239.214.85]) by svr1.postgresql.org (Postfix) with ESMTP id E0161D1B18A for ; Tue, 29 Jun 2004 13:17:43 -0300 (ADT) Received: from fduch by dyatel.antar.bryansk.ru with local (Exim 4.34 (FreeBSD)) id 1BfLIk-000J33-Lr for pgsql-sql@postgresql.org; Tue, 29 Jun 2004 20:17:42 +0400 Date: Tue, 29 Jun 2004 20:17:42 +0400 From: "Alexander M. Pravking" To: pgsql-sql@postgresql.org Subject: UPDATE ... WHERE (subselect on the same table) Message-ID: <20040629161742.GA73173@dyatel.antar.bryansk.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=koi8-r Content-Disposition: inline User-Agent: Mutt/1.4.2.1i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/291 X-Sequence-Number: 18236 I was looking for how can I give an alias for the table being updated (something like UPDATE table_name table_alias SET ...), but the current syntax doesn't allow that. What I need is to: fduch=# SELECT * from test order by typ, name; typ | name | x -----+------+--- 1 | bar | 1 | baz | 1 | foo | 2 | baz | 2 | foo | (5 rows) fduch=# UPDATE test SET x = 't' fduch-# where typ = 1 and exists ( fduch(# SELECT 1 from test t2 fduch(# where t2.typ = 2 and t2.name = test.name fduch(# ); UPDATE 2 fduch=# SELECT * from test order by typ, name; typ | name | x -----+------+--- 1 | bar | 1 | baz | t 1 | foo | t 2 | baz | 2 | foo | (5 rows) So I have two questions: Q1, cognitive. Why the alias for the updated table is restricted? Is there any reason for that or it's just not implemented? Q2, vital. Can I be sure that the syntax I used here will work correctly, i.e. will the "test.name" always refer the column in outer table, not inner (t2)? Thanks in advance. -- Fduch M. Pravking