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 89D06D1C7F7 for ; Fri, 15 Aug 2003 04:52:22 +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 82979-02 for ; Fri, 15 Aug 2003 01:52:12 -0300 (ADT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 774E4D1C7F3 for ; Fri, 15 Aug 2003 01:52:11 -0300 (ADT) Received: (qmail 27805 invoked by uid 500); 15 Aug 2003 04:57:45 -0000 Date: Thu, 14 Aug 2003 23:57:45 -0500 From: Bruno Wolff III To: Slawek Jarosz Cc: pgsql-sql@postgresql.org Subject: Re: Optional join Message-ID: <20030815045745.GA27468@wolff.to> Mail-Followup-To: Slawek Jarosz , pgsql-sql@postgresql.org 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: 200308/187 X-Sequence-Number: 14803 On Thu, Aug 14, 2003 at 10:40:02 -0400, 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 > > Doable? You use outer joins to do this. Something like: SELECT table1.*, table2.pk2 FROM table1 left join table2 on table2.f2 = table1.pk1;