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 A22B1329E25 for ; Mon, 27 Sep 2004 01:21:21 +0100 (BST) 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 41009-06 for ; Mon, 27 Sep 2004 00:21:13 +0000 (GMT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id 5BE5D329D14 for ; Mon, 27 Sep 2004 01:21:12 +0100 (BST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 246D235129; Sun, 26 Sep 2004 17:21:13 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 22D5135125; Sun, 26 Sep 2004 17:21:13 -0700 (PDT) Date: Sun, 26 Sep 2004 17:21:13 -0700 (PDT) From: Stephan Szabo To: Cris Carampa Cc: pgsql-sql@postgresql.org Subject: Re: where clause on a left outer join In-Reply-To: Message-ID: <20040926171842.A46859@megazone.bigpanda.com> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/233 X-Sequence-Number: 19079 On Wed, 22 Sep 2004, Cris Carampa wrote: > Hello, let's suppose I have the following tables: > > create table parent ( > parent_id numeric primary key, > parent_data text > ) ; > > create table stuff ( > stuff_id numeric primary key, > parent_id numeric references parent, > stuff_data text > ) ; > > And the following data: > > crisdb=> select * from parent; > parent_id | parent_data > -----------+------------- > 1 | aaa > 2 | bbb > 3 | ccc > (3 rows) > > crisdb=> select * from stuff; > stuff_id | parent_id | staff_data > ----------+-----------+------------ > 1 | 1 | xxx > 2 | 1 | yyy > 3 | 1 | zzz > (3 rows) > > I wish to write a query that returns all rows from "parent" and, beside > of them, staff data with stuff_id=1 if available, otherwise null. > > The following query: > > select > par.parent_id, > stu.stuff_data > from > parent par left outer join stuff stu > on ( > par.parent_id = stu.parent_id > ) > where > stu.stuff_id = 1 > ; I think on (par.parent_id = stu.parent_id and stu.stuff_id=1) will give the join you want.