Re: Multiple Selects

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: David Slayton <dslayton(at)mhsoftware(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Multiple Selects
Date: 2000-09-11 14:44:16
Message-ID: 13804.968683456@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

David Slayton <dslayton(at)mhsoftware(dot)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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Jochen Weyermanns 2000-09-11 14:48:57 Re: cat infile | translate all occurences of "NOT NULL" to ""
Previous Message Tamsin 2000-09-11 14:36:32 RE: cat infile | translate all occurences of "NOT NULL" to ""