X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 3D61DD1B53A for ; Fri, 15 Aug 2003 04:41:07 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 70833-03 for ; Fri, 15 Aug 2003 01:40:57 -0300 (ADT) Received: from megazone.bigpanda.com (45.mufb.snfc.sffca01r18.dsl.att.net [12.103.249.45]) by svr1.postgresql.org (Postfix) with ESMTP id 58E38D1B524 for ; Fri, 15 Aug 2003 01:40:56 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 632C7D613; Thu, 14 Aug 2003 21:40:56 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 58CDB5C0B; Thu, 14 Aug 2003 21:40:56 -0700 (PDT) Date: Thu, 14 Aug 2003 21:40:56 -0700 (PDT) From: Stephan Szabo To: Slawek Jarosz Cc: Subject: Re: Optional join In-Reply-To: Message-ID: <20030814213916.S5124-100000@megazone.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/188 X-Sequence-Number: 14804 On Thu, 14 Aug 2003, Slawek Jarosz wrote: > Hi, > > I trying to write a query that will join 2 tables. Here's the concept: > Table 1: table1, primary key pk1 Table 2: table2, primary key pk2 > > One of the fields (f2) in table2 contains either the primary key of > table1 or a NULL value. So normally a pretty basic query: > > SELECT table1.*, table2.pk2 FROM table1, table2 WHERE table2.f2 = > table1.pk1; > > BUT what I would like to do is show all records of Table 1 even if > there is no match in Table 2. Meaning that the reults could be > table1... table2.pk2 > table1... NULL See outer joins. Something like: SELECT table1.*,table2.pk FROM table1 LEFT OUTER JOIN table on (table2.f2=table1.pk1);