Received: from sss.pgh.pa.us ([192.204.191.242]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f6KFmIa65114 for ; Fri, 20 Jul 2001 11:48:18 -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.4/8.11.4) with ESMTP id f6KFllC03694; Fri, 20 Jul 2001 11:47:47 -0400 (EDT) To: "Josh Berkus" cc: Gary Stainburn , "pgsql-sql" Subject: Re: multiple lookup per row In-reply-to: References: Comments: In-reply-to "Josh Berkus" message dated "Fri, 20 Jul 2001 07:41:22 -0700" Date: Fri, 20 Jul 2001 11:47:47 -0400 Message-ID: <3691.995644067@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200107/279 X-Sequence-Number: 4076 "Josh Berkus" writes: > ... LEFT OUTER JOIN ... Another way is correlated subselects in the output list: SELECT mid, name, address, (SELECT phone FROM phones WHERE members.mid = phones.mid and ptype = 'home') AS home_phone, (SELECT phone FROM phones WHERE members.mid = phones.mid and ptype = 'work') AS work_phone, (SELECT phone FROM phones WHERE members.mid = phones.mid and ptype = 'cell') AS cell_phone FROM members; With either of these approaches, you'll get NULLs for cases where the member has no phone number of the given type. However, what you ought to think about is what happens if the member has more than one phone number of a single type. With the outer join you will get multiple output rows for that member, which is likely not what you want. With my way, you'd get an execution error, which is definitely not what you want... but you could patch it by including LIMIT 1 in the sub-SELECTs, and perhaps also an ORDER BY to determine *which* phone number is the single one shown. BTW, I second Josh' recommendation of "SQL for Smarties". regards, tom lane