X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id C409DD1E806 for ; Sun, 18 Jan 2004 22:00:09 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 59234-03 for ; Sun, 18 Jan 2004 17:59:40 -0400 (AST) Received: from jalapeno.crazydogs.org (jalapeno.jellybean.co.uk [212.78.70.100]) by svr1.postgresql.org (Postfix) with ESMTP id 418BED1E805 for ; Sun, 18 Jan 2004 17:59:37 -0400 (AST) Received: from rp by jalapeno.crazydogs.org with local (Exim 3.36 #1 (Debian)) id 1AiKuw-0003NR-00 for ; Sun, 18 Jan 2004 21:57:14 +0000 Date: Sun, 18 Jan 2004 21:57:14 +0000 From: Richard Poole To: pgsql-sql@postgresql.org Subject: Re: Left joins with multiple tables Message-ID: <20040118215714.GA11708@guests.deus.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.4i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/155 X-Sequence-Number: 16548 On Sat, Jan 17, 2004 at 02:30:01AM +0000, Colin Fox wrote: > For each person in the people table, they may or may not have a record in > a, may or may not have a record in b, and may or may not have a record in > c. ... > But I'd like to be able to do something like: > > select > id, name, a.field1, b.field2, c.field3 > from > people p left outer join a on a.person_id = p id, > people p left outer join b on b.person_id = p.id, > people p left outer join c on c.person_id = p.id; You can just chain the joins and the Right Thing will happen: SELECT id, name, a.field1, b.field2, c.field3 FROM people p LEFT OUTER JOIN a ON (p.id = a.person_id) LEFT OUTER JOIN a ON (p.id = b.person_id) LEFT OUTER JOIN a ON (p.id = c.person_id) I'm not sure that this behaviour is mandated by the SQL standard; a certain other popular open source database-like product interprets the same construction differently. But it does do what you want in postgres. Richard