X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id BC116D8105 for ; Thu, 15 Sep 2005 10:42:17 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 31302-01 for ; Thu, 15 Sep 2005 13:42:10 +0000 (GMT) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by svr1.postgresql.org (Postfix) with ESMTP id E7DEFD6ECA for ; Thu, 15 Sep 2005 10:42:07 -0300 (ADT) Received: from website.brandywine.com (mailcheck.brandywine.com [146.145.113.9]) by svr4.postgresql.org (Postfix) with ESMTP id 4A4C65AF844 for ; Thu, 15 Sep 2005 13:42:12 +0000 (GMT) X-eSCM-MailFrom: Received: from BAM_EXCHANGE (10.1.0.100) by website.brandywine.com (10.1.0.9); Thu, 15 September 2005 09:44:10 -0400 Received: from [10.1.0.60] (gen-wayne [10.1.0.60]) by bam_exchange.brandywine.com with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id R6ZYXMDF; Thu, 15 Sep 2005 09:42:36 -0400 Message-ID: <43297A80.9030008@brandywine.com> Date: Thu, 15 Sep 2005 09:43:28 -0400 From: Daryl Richter Reply-To: daryl@brandywine.com Organization: Brandywine Asset Management User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 To: fmiddleton@verizon.net, pgsql-sql@postgresql.org Subject: Re: showing multiple REFERENCE details of id fields in single References: <4328CA49.7030409@verizon.net> In-Reply-To: <4328CA49.7030409@verizon.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 required=5 tests=[none] X-Spam-Level: X-Archive-Number: 200509/158 X-Sequence-Number: 22705 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? > create table classes( id SERIAL PRIMARY KEY, course_title TEXT, course_code TEXT ); go insert into classes( course_title, course_code ) values( 'A', 'A1' ); insert into classes( course_title, course_code ) values( 'B', 'B1' ); insert into classes( course_title, course_code ) values( 'C', 'C1' ); go create table class_prerequisite_bindings( id SERIAL, class_id INTEGER REFERENCES classes(id), prerequisite INTEGER REFERENCES classes(id) ); go insert into class_prerequisite_bindings( class_id, prerequisite ) values( 1, 2 ); insert into class_prerequisite_bindings( class_id, prerequisite ) values( 1, 3 ); go SELECT a.id AS class_id, c.id AS prerequisite_class FROM classes a JOIN class_prerequisite_bindings b ON b.class_id = a.id JOIN classes c ON c.id = b.prerequisite WHERE a.id = 1; go > Ferindo > > ---------------------------(end of broadcast)--------------------------- > TIP 5: don't forget to increase your free space map settings -- Daryl "We want great men who, when fortune frowns, will not be discouraged." -- Colonel Henry Knox, 1776