X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 3BB9852806 for ; Tue, 28 Jun 2005 09:47:41 -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 18471-05 for ; Tue, 28 Jun 2005 12:47:31 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id AABFE5288B for ; Tue, 28 Jun 2005 09:47:30 -0300 (ADT) Received: (qmail 21243 invoked by uid 500); 28 Jun 2005 12:47:33 -0000 Date: Tue, 28 Jun 2005 07:47:33 -0500 From: Bruno Wolff III To: Mukesh Ghatiya Cc: pgsql-sql@postgresql.org Subject: Re: cross-table reference Message-ID: <20050628124733.GB20566@wolff.to> Mail-Followup-To: Bruno Wolff III , Mukesh Ghatiya , pgsql-sql@postgresql.org References: <36586775D676C443A856A00683526649A0C797@gaugeboson.jnpr.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <36586775D676C443A856A00683526649A0C797@gaugeboson.jnpr.net> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.007 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/337 X-Sequence-Number: 21938 On Wed, Jun 22, 2005 at 14:56:08 +0530, Mukesh Ghatiya wrote: > Hi, > > I need to perform a query similar to > > SELECT table1.a.b.x table1.a.c.x from table1; > > > In this case "a" is an attribute in 'table1' and is a foreign key to > other table2 which has 'b', and 'c' as attributes, which again are > foreign keys to table3. > > > Is there any simple way of doing this other than using the complicated > nested joins which would include aliases also. > > SELECT table31.x table32.x > FROM table1, table3 AS table31, table3 AS table32 > WHERE table1.a = table2.id > AND table2.b = table31.id > AND table2.c = table32.id This isn't really that complicated. (Though note you left table2 out of the from item list.) You might be able to reasonable performance and simplify the select statement by defining two functions to select from table2 and table3. If this were written in language SQL the query might even end up with essentially the same plan.