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 40D28D1B8A4 for ; Sat, 16 Aug 2003 16:58:17 +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 16997-03 for ; Sat, 16 Aug 2003 13:58:07 -0300 (ADT) Received: from p15140235.pureserver.info (sql-info.de [217.160.166.164]) by svr1.postgresql.org (Postfix) with ESMTP id 731F6D1B8A3 for ; Sat, 16 Aug 2003 13:58:06 -0300 (ADT) Received: from iwate.sql-info.de (pD9EB17D1.dip.t-dialin.net [217.235.23.209]) (using SSLv3 with cipher RC4-MD5 (128/128 bits)) (No client certificate requested) by p15140235.pureserver.info (Postfix) with ESMTP id 2ED421B76; Sat, 16 Aug 2003 18:58:06 +0200 (CEST) From: Ian Barwick To: "Slawek Jarosz" , pgsql-sql@postgresql.org Subject: Re: Optional join Date: Sat, 16 Aug 2003 18:56:57 +0200 User-Agent: KMail/1.5.1 References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200308161856.57476.barwick@gmx.net> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/201 X-Sequence-Number: 14817 On Thursday 14 August 2003 16:40, 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 need an OUTER JOIN, see e.g. http://www.postgresql.org/docs/7.3/static/tutorial-join.html Ian Barwick barwick@gmx.net