Received: from sss.pgh.pa.us ([216.151.103.158]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f36EkX832684 for ; Fri, 6 Apr 2001 10:46:33 -0400 (EDT) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.3/8.11.3) with ESMTP id f36EkDw25389; Fri, 6 Apr 2001 10:46:13 -0400 (EDT) To: juerg.rietmann@pup.ch cc: pgsql-sql@postgresql.org Subject: Re: how to do this join ? In-reply-to: References: Comments: In-reply-to juerg.rietmann@pup.ch message dated "Fri, 06 Apr 2001 14:10:15 +0200" Date: Fri, 06 Apr 2001 10:46:13 -0400 Message-ID: <25386.986568373@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200104/97 X-Sequence-Number: 2437 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