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 A11625292A for ; Wed, 17 Aug 2005 14:44:10 -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 65784-06 for ; Wed, 17 Aug 2005 17:44:06 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 77D52528BB for ; Wed, 17 Aug 2005 14:44:05 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j7HHhwmL006756 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 17 Aug 2005 11:44:02 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j7HHhvFo034197; Wed, 17 Aug 2005 11:43:57 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j7HHhtZL034196; Wed, 17 Aug 2005 11:43:55 -0600 (MDT) (envelope-from mfuhr) Date: Wed, 17 Aug 2005 11:43:55 -0600 From: Michael Fuhr To: Lane Van Ingen Cc: pgsql-sql@postgresql.org Subject: Re: Is it This Join Condition Do-Able? Message-ID: <20050817174355.GA34164@winnie.fuhr.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.109 required=5 tests=[AWL=-0.098, UPPERCASE_25_50=0.207] X-Spam-Level: X-Archive-Number: 200508/124 X-Sequence-Number: 22352 On Wed, Aug 17, 2005 at 12:54:50PM -0400, Lane Van Ingen wrote: > Given three tables: a, b, c ; each consist of a 'keyfld' and a field called > 'foo': > tbl a tbl b tbl c > --------- --------- --------- > a.keyfld b.keyfld c.keyfld > a.foo1 b.foo2 c.foo3 > > I want to always return all of tbl a; and I want to return b.foo2 and c.foo3 if > they can be joined to based on keyfld.a; I know that it will involve a LEFT OUTER > JOIN on table a, but have not seen any examples of joins like this on 3 or more > tables. Does this example do what you want? CREATE TABLE a (keyfld integer, foo1 text); CREATE TABLE b (keyfld integer, foo2 text); CREATE TABLE c (keyfld integer, foo3 text); INSERT INTO a VALUES (1, 'a1'); INSERT INTO a VALUES (2, 'a2'); INSERT INTO a VALUES (3, 'a3'); INSERT INTO a VALUES (4, 'a4'); INSERT INTO b VALUES (1, 'b1'); INSERT INTO b VALUES (4, 'b4'); INSERT INTO c VALUES (2, 'c2'); INSERT INTO c VALUES (4, 'c4'); SELECT a.keyfld, a.foo1, b.foo2, c.foo3 FROM a LEFT OUTER JOIN b USING (keyfld) LEFT OUTER JOIN c USING (keyfld); keyfld | foo1 | foo2 | foo3 --------+------+------+------ 1 | a1 | b1 | 2 | a2 | | c2 3 | a3 | | 4 | a4 | b4 | c4 (4 rows) -- Michael Fuhr