From: Jackson, DeJuan <djackson@cpsgroup.com>
To: Andy <alewis@mpsi.net>
Cc: PGSQL SQL <pgsql-sql@postgresql.org>
Subject: RE: Two table select (fwd)
Date: Wed, 3 Feb 1999 14:57:26 -0600
Message-ID: <F10BB1FAF801D111829B0060971D839F63FBBA@cpsmail> (raw)
I thought as much... the best way to do a single select, with what you
currently have, would be to use a union all clause.
SELECT r.bldg1, ..., a.bldgname
FROM refferal r, apts a
WHERE r.bldg1 = a.bldgnum
UNION ALL
SELECT r.bldg2, ..., a.bldgname
FROM refferal r, apts a
WHERE r.bldg2 = a.bldgnum
UNION ALL
.
.
.
Now if you normalize your data into three table you could make it a
single select (w/o UNION) with a three table join. BTW it's a good idea
to place non-variable length fields before variable length ones in a
table definition.
i.e.
table = referral
+----------------------------------+----------------------------------+-
------+
| ref_id | int4 |
4 |
| rstate | varchar() |
2 |
| rrif | varchar() |
10 |
| rphone | varchar() |
20 |
| rfirstname | varchar() |
20 |
| rlastname | varchar() |
25 |
+----------------------------------+----------------------------------+-
------+
table = refbldg
+----------------------------------+----------------------------------+-
------+
| Field | Type |
Length|
+----------------------------------+----------------------------------+-
------+
| ref_id | int4 |
4 |
| rbldg | varchar() |
10 |
| rbldgcomment | text |
var |
+----------------------------------+----------------------------------+-
------+
table = apts
+----------------------------------+----------------------------------+-
------+
| Field | Type |
Length|
+----------------------------------+----------------------------------+-
------+
| bldgnum | varchar() |
10 |
| bldgname | varchar() |
35 |
...
+----------------------------------+----------------------------------+-
------+
SELECT r.*, rb.refbldg, rb.rbldgcomment, a.bldgname
FROM referral r. refbldg rb, apts a
WHERE r.ref_if = rb.ref_if AND
rb.rbldg = a.bldgnum
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: djackson@cpsgroup.com, alewis@mpsi.net
Subject: Re: Two table select (fwd)
In-Reply-To: <F10BB1FAF801D111829B0060971D839F63FBBA@cpsmail>
References: <F10BB1FAF801D111829B0060971D839F63FBBA@cpsmail>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox