Re: Optional join

From: Ian Barwick <barwick(at)gmx(dot)net>
To: "Slawek Jarosz" <Jarosz(dot)S(at)ems-t(dot)ca>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Optional join
Date: 2003-08-16 16:56:57
Message-ID: 200308161856.57476.barwick@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(at)gmx(dot)net

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Rod Taylor 2003-08-17 02:47:22 Re: Do it exist?
Previous Message Tom Lane 2003-08-16 12:36:11 Re: About primary keys -- made some progress