From: Thomas O'Connell <tfo@sitening.com>
To: fmiddleton@verizon.net
Cc: pgsql-sql@postgresql.org
Subject: Re: showing multiple REFERENCE details of id fields in single query that share the same table
Date: Thu, 15 Sep 2005 00:16:12 -0500
Message-ID: <AE7033A4-E297-4BE6-A404-EBF9C318735D@sitening.com> (raw)
In-Reply-To: <4328CA49.7030409@verizon.net>
References: <4328CA49.7030409@verizon.net>
On Sep 14, 2005, at 8:11 PM, Ferindo Middleton Jr wrote:
> I have a table which has two id fields which REFERENCE data back at
> another table. It's setup like this:
>
> class_prerequisite_bindings(id SERIAL, class_id INTEGER
> REFERENCES classes(id), prerequisite INTEGER REFERENCES classes
> (id))
>
> The classes table is like this:
> classes(id SERIAL, course_title TEXT, course_code TEXT)
>
> I have the following query:
> SELECT * FROM class_prerequisite_bindings, classes WHERE
> class_prerequisite_bindings.class_id = 64 AND
> class_prerequisite_bindings.class_id = classes.id;
>
> If I run the query above, the result will only give me info about
> the class_id field matching id 64 back in the classes table.
> PROBLEM: I want this query to also show the info about the
> prerequisite field which would also have info at the classes table.
> This query will only show the course_title and course_code of the
> class_id but I need this for the prerequisite field as well. I
> think I need to do a JOIN, but I don't understand how. How can I do
> this?
>
> Ferindo
This isn't the prettiest query in the world, but it's a starting
point. The RIGHT JOIN allows you to include the columns you want for
prerequisites that don't themselves have prerequisites.
SELECT *
FROM class_prerequisite_bindings, classes
WHERE class_prerequisite_bindings.class_id = 64
AND class_prerequisite_bindings.class_id = classes.id
UNION ALL
SELECT *
FROM class_prerequisite_bindings
RIGHT JOIN classes
ON classes.id = class_prerequisite_bindings.class_id
WHERE classes.id = (
SELECT prerequisite
FROM class_prerequisite_bindings
WHERE class_id = 64
);
--
Thomas F. O'Connell
Co-Founder, Information Architect
Sitening, LLC
Strategic Open Source: Open Your i™
http://www.sitening.com/
110 30th Avenue North, Suite 6
Nashville, TN 37203-6320
615-469-5150
615-469-5151 (fax)
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: tfo@sitening.com, fmiddleton@verizon.net
Subject: Re: showing multiple REFERENCE details of id fields in single query that share the same table
In-Reply-To: <AE7033A4-E297-4BE6-A404-EBF9C318735D@sitening.com>
References: <4328CA49.7030409@verizon.net> <AE7033A4-E297-4BE6-A404-EBF9C318735D@sitening.com>
* 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