pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christian Fowler <spider@viovio.com>
To: Roderick A. Anderson <raa@mailporter.net>
Cc: Jaime Casanova <systemguards@yahoo.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: joins INNER, OUTER, LEFT, RIGHT, FULL, ...
Date: Wed, 24 Nov 2004 01:32:37 -0500 (EST)
Message-ID: <Pine.LNX.4.61.0411240130250.20105@leda.steelsun.com> (raw)
In-Reply-To: <41A41CFD.1020109@mailporter.net>
References: <20041124050641.86985.qmail@web50004.mail.yahoo.com>
	<41A41CFD.1020109@mailporter.net>

On Tue, 23 Nov 2004, Roderick A. Anderson wrote:

> Jaime Casanova wrote:
>
>> http://www.postgresql.org/docs/7.4/static/queries-table-expressions.html#QUERIES-JOIN
>> 
> Been there, done that.  Bought several tee-shirts.
>
>> What do you mean with *complex joins*?
>> 
>
> SELECT first, last, username || '@' || dom.domain as emailaddress
> FROM cust_main cm
>  LEFT OUTER JOIN cust_pkg    cp ON ( cm.custnum = cp.custnum )
>  LEFT OUTER JOIN cust_svc    cs ON ( cp.pkgnum  = cs.pkgnum )
>  LEFT OUTER JOIN svc_email   em ON ( cs.svcnum  = em.svcnum )
>  LEFT OUTER JOIN svc_domain dom ON ( em.domain  = dom.svcnum )
> WHERE zip = '99999'
>   AND username || '@' || dom.domain != '@'
>
> Which worked until I added one more table with a one-to-one relation to 
> cust_main.custnum.  Then I got several tuples (2+) for each row above.

try:

SELECT first, last, username || '@' || dom.domain as emailaddress
FROM cust_main cm
  INNER JOIN one_more om ON ( om.custnum = cm.custnum )
  LEFT OUTER JOIN cust_pkg    cp ON ( cm.custnum = cp.custnum )
  LEFT OUTER JOIN cust_svc    cs ON ( cp.pkgnum  = cs.pkgnum )
  LEFT OUTER JOIN svc_email   em ON ( cs.svcnum  = em.svcnum )
  LEFT OUTER JOIN svc_domain dom ON ( em.domain  = dom.svcnum )
WHERE zip = '99999'
   AND username || '@' || dom.domain != '@'

or

SELECT first, last, username || '@' || dom.domain as emailaddress
FROM one_more om, cust_main cm
  LEFT OUTER JOIN cust_pkg    cp ON ( cm.custnum = cp.custnum )
  LEFT OUTER JOIN cust_svc    cs ON ( cp.pkgnum  = cs.pkgnum )
  LEFT OUTER JOIN svc_email   em ON ( cs.svcnum  = em.svcnum )
  LEFT OUTER JOIN svc_domain dom ON ( em.domain  = dom.svcnum )
WHERE zip = '99999'
   AND om.custnum=cm.custnum
   AND username || '@' || dom.domain != '@'


The explicit join syntax was freaky for me too at first,  but after 
several years, I prefer it now, since you can easily control your left 
outer joins


[ \ /
[ >X<   Christian Fowler      | spider AT viovio.com
[ / \   http://www.viovio.com | http://www.tikipro.org



view thread (4+ messages)

Message-ID: <Pine.LNX.4.61.0411240130250.20105@leda.steelsun.com>
Permalink:  ../Pine.LNX.4.61.0411240130250.20105@leda.steelsun.com/
Also on:    postgresql.org/message-id/Pine.LNX.4.61.0411240130250.20105@leda.steelsun.com

 · 

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-admin@postgresql.org
  Cc: spider@viovio.com, raa@mailporter.net, systemguards@yahoo.com
  Subject: Re: joins INNER, OUTER, LEFT, RIGHT, FULL, ...
  In-Reply-To: <Pine.LNX.4.61.0411240130250.20105@leda.steelsun.com>
  References: <20041124050641.86985.qmail@web50004.mail.yahoo.com> <41A41CFD.1020109@mailporter.net> <Pine.LNX.4.61.0411240130250.20105@leda.steelsun.com>

* 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