Re: Optional join

From: David Fetter <shackle(at)fetter(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Optional join
Date: 2003-08-15 05:50:02
Message-ID: nXidnVVfXr8X6aGiXTWc-w@speakeasy.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Slawek Jarosz" <Jarosz(dot)S(at)ems-t(dot)ca> 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(at)fetter(dot)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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message cristi 2003-08-15 09:32:36 sequence
Previous Message Bruno Wolff III 2003-08-15 04:57:45 Re: Optional join