Re: Is it This Join Condition Do-Able? - Mailing list pgsql-sql

From Jeremy Semeiks
Subject Re: Is it This Join Condition Do-Able?
Date
Msg-id 20050817174450.GU28158@farviolet.farviolet.com
Whole thread Raw
In response to Is it This Join Condition Do-Able?  ("Lane Van Ingen" <lvaningen@esncc.com>)
List pgsql-sql
On Wed, Aug 17, 2005 at 12:54:50PM -0400, Lane Van Ingen wrote:
> Given three tables: a, b, c ; each consist of a 'keyfld' and a field called
> 'foo':
>      tbl a       tbl b         tbl c
>    ---------   ---------     ---------
>    a.keyfld    b.keyfld       c.keyfld
>    a.foo1      b.foo2         c.foo3
> 
> I want to always return all of tbl a; and I want to return b.foo2 and c.foo3
> if
> they can be joined to based on keyfld.a; I know that it will involve a LEFT
> OUTER
> JOIN on table a, but have not seen any examples of joins like this on 3 or
> more
> tables.
> 
> select a.keyfld, a.foo1, b.foo2, c.foo3
> from a, b, c
> where a.keyfld = <some value>
> and   a.keyfld = b.keyfld
> and   a.keyfld = c.keyfld;
> 
> Results could look like this:
>   a.keyfld  a.foo1   b.foo2  c.foo3
>     xxxx     xxxx     xxxx    (null)
>     xxxx     xxxx    (null)    xxxx
>     xxxx     xxxx    (null)   (null)
>     xxxx     xxxx     xxxx     xxxx

Just use two left joins:

select a.keyfld, a.foo1, b.foo2, c.foo3
from a
left join b on a.keyfld = b.keyfld
left join c on a.keyfld = c.keyfld
where a.keyfld = <some value>;

HTH,
Jeremy


pgsql-sql by date:

Previous
From: "A. Kretschmer"
Date:
Subject: Re: [despammed] converting varchar to integer
Next
From: Roger Motorola
Date:
Subject: Locating ( FKs ) References to a Primary Key