Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 9C236475F56 for ; Mon, 12 Aug 2002 11:11:44 -0400 (EDT) Received: from web40013.mail.yahoo.com (web40013.mail.yahoo.com [66.218.78.53]) by postgresql.org (Postfix) with SMTP id B38CC475D9C for ; Mon, 12 Aug 2002 11:11:43 -0400 (EDT) Message-ID: <20020812151148.26186.qmail@web40013.mail.yahoo.com> Received: from [210.23.115.65] by web40013.mail.yahoo.com via HTTP; Mon, 12 Aug 2002 08:11:48 PDT Date: Mon, 12 Aug 2002 08:11:48 -0700 (PDT) From: Ludwig Lim Subject: Re: Need Help for select To: Andre Schubert Cc: PostgreSQL Mailing List In-Reply-To: <20020812150640.4acd538b.andre@km3.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200208/188 X-Sequence-Number: 9034 --- Andre Schubert wrote: > Hi all, > > i need help to build a select query or > plpgsql-fucntion > for the following tables. >> Is it possible to build a select query that selects > d.name for each a.name where > a.id = b.a_id and d.id = c.d_id and each b.c_id must > exist in c.b_id. > > Example: > a: b: c : d: > id | name a_id | c_id b_id | d_id id | > name > ----|------- -------|----- -------|----- > -----|-------- > 1 | A_Name1 1 | 1 1 | 1 1 | > D_Name1 > 2 | A_Name2 1 | 2 2 | 1 2 | > D_Name2 > 3 | A_Name3 2 | 1 3 | 2 3 | > D_Name3 > 4 | A_Name4 3 | 3 4 | 2 > 3 | 4 5 | 3 > 4 | 5 > > i wish to have to following result: > --------|-------- > A_Name1 | D_Name1 > A_Name3 | D_Name2 > A_Name4 | D_Name3 > > I hope someone could understand the problem You can use views to to simplify complicated queries Create a view that will join table A & B Create view view_ab(name,id) as select name,c_id from a,b where id = c_id; Create a view that will join table C & D Create view view_cd(name2,id2) as select name,b_id from c,d where id=d_id; Create a query that will join the views "view_ab" and "view_cd" Select name,name2 from view_ab,view_cd where id=id2; __________________________________________________ Do You Yahoo!? HotJobs - Search Thousands of New Jobs http://www.hotjobs.com