pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Gerhard Dieringer <DieringG@eba-haus.de>
To: Henry Lafleur <HLafleur@phoenixforge.com>
Cc: " <"<pgsql-sql@postgresql.org>
Subject: Antw: RE: join if there, blank if not
Date: Thu, 13 Jul 2000 09:01:44 +0200
Message-ID: <s96d8580.045@kopo001> (raw)


Henry Lafleur wrote:

> ...
> What I have always had trouble with, though, is if you have multiple fields
> for a primary key. For example, if a customer master table also had ship-to
> locations as the key and you wanted to get all customers and any orders for
> that customer, in rough ANSI SQL it would be:
> 
> SELECT c.cust_number, c.ship_to, o.item
> FROM cust c LEFT OUTER JOIN orders o ON c.cust_number = o.cust_number AND
> c.ship_to = o.ship_to
> 
> then, in the union, it is not clear how to do it:
> 
> SELECT c.cust_number, c.ship_to, o.item
> FROM cust c, orders o
> WHERE c.cust_number = o.cust_number AND c.ship_to = o.ship_to
> UNION
> SELECT cust_number, ship_to, NULL AS item
> FROM cust
> WHERE ???
> ...

I don't see any problems with multiple fields. See the following example:

Outer join:
SELECT  tab_a.k1, tab_a.k2, tab_a.a_txt, tab_b.b_txt
FROM tab_a LEFT JOIN tab_b ON (tab_a.k2 = tab_b.k2) AND (tab_a.k1 = tab_b.k1);

Simulated outer join:
SELECT tab_a.k1, tab_a.k2, tab_a.a_txt, tab_b.b_txt
FROM tab_a , tab_b  WHERE (tab_a.k2 = tab_b.k2) AND (tab_a.k1 = tab_b.k1)
UNION
SELECT tab_a.k1, tab_a.k2, tab_a.a_txt, NULL
FROM tab_a WHERE (tab_a.k1 NOT IN (SELECT tab_b.k1 FROM tab_b))
                        OR (tab_a.k2 NOT IN (SELECT tab_b.k2 FROM tab_b));

Gerhard





Message-ID: <s96d8580.045@kopo001>
Permalink:  ../s96d8580.045@kopo001/
Also on:    postgresql.org/message-id/s96d8580.045@kopo001

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: DieringG@eba-haus.de, HLafleur@phoenixforge.com, "<pgsql-sql@postgresql.org
  Subject: Re: join if there, blank if not
  In-Reply-To: <s96d8580.045@kopo001>
  References: <s96d8580.045@kopo001>

* 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