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 8D422D86A4 for ; Thu, 15 Sep 2005 02:30:02 -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 01887-03 for ; Thu, 15 Sep 2005 05:29:51 +0000 (GMT) Received: by svr1.postgresql.org (Postfix, from userid 1001) id 9914CD919C; Thu, 15 Sep 2005 03:46:41 -0300 (ADT) Received: from window.monsterlabs.com (window.monsterlabs.com [216.183.105.176]) by svr1.postgresql.org (Postfix) with SMTP id B3EB6D8FF8 for ; Thu, 15 Sep 2005 02:16:39 -0300 (ADT) Received: (qmail 613 invoked from network); 15 Sep 2005 05:15:03 -0000 Received: from pcp0012204803pcs.blairblvd.tn.nash.comcast.net (HELO ?192.168.15.106?) (69.245.49.69) by 0 with SMTP; 15 Sep 2005 05:15:03 -0000 In-Reply-To: <4328CA49.7030409@verizon.net> References: <4328CA49.7030409@verizon.net> Mime-Version: 1.0 (Apple Message framework v734) Content-Type: text/plain; charset=WINDOWS-1252; delsp=yes; format=flowed Message-Id: Cc: pgsql-sql@postgresql.org Content-Transfer-Encoding: quoted-printable From: Thomas O'Connell 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 To: fmiddleton@verizon.net X-Mailer: Apple Mail (2.734) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.016 required=5 tests=[AWL=0.016] X-Spam-Level: X-Archive-Number: 200509/155 X-Sequence-Number: 22702 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 =20= > another table. It's setup like this: > > class_prerequisite_bindings(id SERIAL, class_id INTEGER =20 > REFERENCES classes(id), prerequisite INTEGER REFERENCES classes=20= > (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 =20 > class_prerequisite_bindings.class_id =3D 64 AND =20 > class_prerequisite_bindings.class_id =3D classes.id; > > If I run the query above, the result will only give me info about =20 > the class_id field matching id 64 back in the classes table. =20 > PROBLEM: I want this query to also show the info about the =20 > prerequisite field which would also have info at the classes table. =20= > This query will only show the course_title and course_code of the =20 > class_id but I need this for the prerequisite field as well. I =20 > think I need to do a JOIN, but I don't understand how. How can I do =20= > this? > > Ferindo This isn't the prettiest query in the world, but it's a starting =20 point. The RIGHT JOIN allows you to include the columns you want for =20 prerequisites that don't themselves have prerequisites. SELECT * FROM class_prerequisite_bindings, classes WHERE class_prerequisite_bindings.class_id =3D 64 AND class_prerequisite_bindings.class_id =3D classes.id UNION ALL SELECT * FROM class_prerequisite_bindings RIGHT JOIN classes ON classes.id =3D class_prerequisite_bindings.class_id WHERE classes.id =3D ( SELECT prerequisite FROM class_prerequisite_bindings WHERE class_id =3D 64 ); -- Thomas F. O'Connell Co-Founder, Information Architect Sitening, LLC Strategic Open Source: Open Your i=99 http://www.sitening.com/ 110 30th Avenue North, Suite 6 Nashville, TN 37203-6320 615-469-5150 615-469-5151 (fax)=