Re: Multiple Selects - Mailing list pgsql-novice

From Tom Lane
Subject Re: Multiple Selects
Date
Msg-id 13804.968683456@sss.pgh.pa.us
Whole thread Raw
In response to Multiple Selects  (David Slayton <dslayton@mhsoftware.com>)
List pgsql-novice
David Slayton <dslayton@mhsoftware.com> writes:
> essentially, i want to select records from the table with one WHERE
> clause, then select a second set from the same table with a different
> WHERE to compose a VIEW.

I think what you are after here is something like

    SELECT *
    FROM mytable AS a, mytable AS b
    WHERE a.f1 = 33 AND b.f2 = 42 AND a.key = b.key ...

Basically, you select from the same table twice, assigning different
aliases to the two entries so that you can distinguish between them
in references from the rest of the query.  I used "a" and "b" here,
but you can use any names you like as table aliases.  The two FROM
entries are then joined just as if they were totally separate tables.

When you just write "SELECT FROM foo", you really get "FROM foo AS foo",
ie, the table's real name is also its alias for that query.

BTW, the "AS" is just a noise word and is often omitted.

            regards, tom lane

pgsql-novice by date:

Previous
From: The Hermit Hacker
Date:
Subject: RE: cat infile | translate all occurences of "NOT NULL" to ""
Next
From: patrick.wolf@Aerojet.com (WOLF, PATRICK)
Date:
Subject: RE: Removing all NOT NULL constraints from multiple tabl es easily