pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: juerg.rietmann@pup.ch
Cc: pgsql-sql@postgresql.org
Subject: Re: how to do this join ?
Date: Fri, 06 Apr 2001 10:46:13 -0400
Message-ID: <25386.986568373@sss.pgh.pa.us> (raw)
In-Reply-To: <OF9E707DA8.649AA3F3-ONC1256A26.00427E3C@pup.ch>
References: <OF9E707DA8.649AA3F3-ONC1256A26.00427E3C@pup.ch>

juerg.rietmann@pup.ch writes:
> select *,
> (select a_typ from auftrag where a_nr=z_a_nr) as typ,
> (select a_t_definition_d from auftrags_typ where a_t_code=typ) as text
> from zylinder

Seems like a very non-SQLish way to proceed.  Instead use joins:

select zylinder.*, a_typ as typ, a_t_definition_d as text
from zylinder, auftrag, auftrags_typ
where a_nr = z_a_nr and a_t_code = a_typ

If there were multiple matches in auftrag or auftrags_typ then this
would yield multiple rows per zylinder row, which you might not want;
but your subselect-based approach is already assuming there are not
multiple matches.

If there's a possibility of *no* matching row, then the first solution
would emit NULLs for the missing auftrag and auftrag_typ values, whereas
the second would emit nothing at all for that zylinder row.  If that's
not what you want, you need to use outer joins (new in 7.1):

select zylinder.*, a_typ as typ, a_t_definition_d as text
from (zylinder left join auftrag on (a_nr = z_a_nr))
     left join auftrags_typ on (a_t_code = a_typ);

			regards, tom lane



view thread (3+ messages)  latest in thread

Message-ID: <25386.986568373@sss.pgh.pa.us>
Permalink:  ../25386.986568373@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/25386.986568373@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, juerg.rietmann@pup.ch
  Subject: Re: how to do this join ?
  In-Reply-To: <25386.986568373@sss.pgh.pa.us>
  References: <OF9E707DA8.649AA3F3-ONC1256A26.00427E3C@pup.ch> <25386.986568373@sss.pgh.pa.us>

* 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