Re: [GENERAL] Simulating an outer join

From: Mike Mascari <mascarm(at)mascari(dot)com>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: PostgreSQL-general <pgsql-general(at)postgreSQL(dot)org>
Subject: Re: [GENERAL] Simulating an outer join
Date: 2000-01-12 17:46:33
Message-ID: 387CBDF9.F5696981@mascari.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Bruce Momjian wrote:
>
> I have been thinking about how to simulate an outer join. It seems the
> best way is to do:
>
> SELECT tab1.col1, tab2.col3
> FROM tab1, tab2
> WHERE tab1.col1 = tab2.col2
> UNION ALL
> SELECT tab1.col1, NULL
> FROM tab1
> WHERE tab1.col1 NOT IN (SELECT tab2.col2 FROM tab2)
>
> Comments? I know someone was asking about this recently.
>

I wouldn't use IN ;-)

SELECT table1.key, table2.value
FROM table1, table2
WHERE table1.key = table2.key
UNION ALL
SELECT table1.key, NULL
FROM table1 WHERE NOT EXISTS
(SELECT table2.key FROM table2 WHERE table1.key = table2.key);

Mike Mascari

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Greg Youngblood 2000-01-12 17:52:52 Rules, triggers, ??? - What is the best way to enforce data-valid ation tests?
Previous Message admin 2000-01-12 17:42:47 rule or trigger on select?