Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id AE7899F99F5 for ; Fri, 9 Nov 2007 15:14:25 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 78842-01-2 for ; Fri, 9 Nov 2007 15:14:06 -0400 (AST) Received: from perhan.alvh.no-ip.org (200-126-109-48.bk7-dsl.surnet.cl [200.126.109.48]) by postgresql.org (Postfix) with ESMTP id DC2F69FCB99 for ; Fri, 9 Nov 2007 15:13:27 -0400 (AST) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 88A2747C7D; Fri, 9 Nov 2007 15:35:59 -0300 (CLST) Date: Fri, 9 Nov 2007 15:35:59 -0300 From: Alvaro Herrera To: Ray Madigan Cc: Pgsql-Sql Subject: Re: Select in From clause Message-ID: <20071109183559.GA7161@alvh.no-ip.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.16 (2007-06-11) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.278 tagged_above=0 required=5 tests=AWL=-1.605, BAYES_50=0.001, RCVD_IN_PBL=0.905, RCVD_IN_SORBS_DUL=0.877, RDNS_DYNAMIC=0.1 X-Spam-Level: X-Archive-Number: 200711/35 X-Sequence-Number: 29563 Ray Madigan wrote: > I have never seen this done before, but it seems like it is supposed to work > from reading the manual. > > I want to be able to get a table name from another table and use it in the > from clause of a select. > > Something like > > SELECT * FROM (SELECT name FROM bar WHERE conditions) AS b WHERE b.condition > = xxx; > > which translates to something like > > SELECT * FROM Dealer AS b WHERE b.zipcode = 12345; No, that's not how it works. The stuff returned by the inner select is a set of rows which can be further operated upon by the outer select. It is not expanded into a table name. One way to construct queries is to build plpgsql functions and use EXECUTE. However, the approach you are using looks like bad practice (read: bad database design). -- Alvaro Herrera http://www.CommandPrompt.com/ The PostgreSQL Company - Command Prompt, Inc.