From: Michael Fuhr <mike@fuhr.org>
To: Lane Van Ingen <lvaningen@esncc.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Is it This Join Condition Do-Able?
Date: Wed, 17 Aug 2005 11:43:55 -0600
Message-ID: <20050817174355.GA34164@winnie.fuhr.org> (raw)
In-Reply-To: <EKEMKEFLOMKDDLIALABIAEHJCBAA.lvaningen@esncc.com>
References: <EKEMKEFLOMKDDLIALABIAEHJCBAA.lvaningen@esncc.com>
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.
Does this example do what you want?
CREATE TABLE a (keyfld integer, foo1 text);
CREATE TABLE b (keyfld integer, foo2 text);
CREATE TABLE c (keyfld integer, foo3 text);
INSERT INTO a VALUES (1, 'a1');
INSERT INTO a VALUES (2, 'a2');
INSERT INTO a VALUES (3, 'a3');
INSERT INTO a VALUES (4, 'a4');
INSERT INTO b VALUES (1, 'b1');
INSERT INTO b VALUES (4, 'b4');
INSERT INTO c VALUES (2, 'c2');
INSERT INTO c VALUES (4, 'c4');
SELECT a.keyfld, a.foo1, b.foo2, c.foo3
FROM a
LEFT OUTER JOIN b USING (keyfld)
LEFT OUTER JOIN c USING (keyfld);
keyfld | foo1 | foo2 | foo3
--------+------+------+------
1 | a1 | b1 |
2 | a2 | | c2
3 | a3 | |
4 | a4 | b4 | c4
(4 rows)
--
Michael Fuhr
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: mike@fuhr.org, lvaningen@esncc.com
Subject: Re: Is it This Join Condition Do-Able?
In-Reply-To: <20050817174355.GA34164@winnie.fuhr.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox