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 22FB7D1C50D for ; Fri, 15 Aug 2003 05:49:38 +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 85183-07 for ; Fri, 15 Aug 2003 02:49:28 -0300 (ADT) Received: from news.hub.org (unknown [64.117.224.194]) by svr1.postgresql.org (Postfix) with ESMTP id 8F907D1B524 for ; Fri, 15 Aug 2003 02:49:27 -0300 (ADT) Received: from news.hub.org (host-64-117-224-194.altec1.com [64.117.224.194] (may be forged)) by news.hub.org (8.12.9/8.12.9) with ESMTP id h7F5nRek056708 for ; Fri, 15 Aug 2003 05:49:27 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id h7F5iRRu056130 for pgsql-sql@postgresql.org; Fri, 15 Aug 2003 05:44:27 GMT From: David Fetter Subject: Re: Optional join X-Newsgroups: comp.databases.postgresql.sql References: User-Agent: tin/1.6.0-20030714 ("Vatersay") (UNIX) (Linux/2.4.20-8 (i686)) Message-ID: Date: Fri, 15 Aug 2003 00:50:02 -0500 Lines: 38 X-Complaints-To: abuse@speakeasy.net X-DMCA-Complaints-To: abuse@speakeasy.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.1 To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/198 X-Sequence-Number: 14814 "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? Yes. SELECT table1.*, table2.pk2 FROM table1 LEFT OUTER JOIN table2 ON (table1.pk1 = table2.pk2); HTH :) Cheers, D -- David Fetter david@fetter.org http://fetter.org/ phone: +1 510 893 6100 cell: +1 415 235 3778 Power over a man's subsistence is power over his will. Alexander Hamilton