Re: Select in From clause

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Ray Madigan <ray(at)madigans(dot)org>
Cc: Pgsql-Sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Select in From clause
Date: 2007-11-09 18:35:59
Message-ID: 20071109183559.GA7161@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Michele Petrazzo - Unipex srl 2007-11-09 19:46:41 functions are returns columns
Previous Message Ray Madigan 2007-11-09 17:21:40 Select in From clause