Received: from superfly.archi-me-des.de ([62.8.212.177]) by postgresql.org (8.11.3/8.11.4) with ESMTP id g17M4rE31235 for ; Thu, 7 Feb 2002 17:04:53 -0500 (EST) (envelope-from frank@joerdens.de) Received: (from frank@localhost) by superfly.archi-me-des.de (8.9.3/8.9.3) id XAA07165; Thu, 7 Feb 2002 23:04:48 +0100 Date: Thu, 7 Feb 2002 23:04:48 +0100 From: Frank Joerdens To: Stephan Szabo Cc: pgsql-sql@postgresql.org, hank@fas-art.com, annette@fas-art.com Subject: Re: Looking for the correct solution for a generic problem. Message-ID: <20020207230448.B6766@superfly.archi-me-des.de> References: <20020207215234.A6766@superfly.archi-me-des.de> <20020207132603.D32753-100000@megazone23.bigpanda.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii User-Agent: Mutt/1.1.2i In-Reply-To: <20020207132603.D32753-100000@megazone23.bigpanda.com>; from sszabo@megazone23.bigpanda.com on Thu, Feb 07, 2002 at 01:31:03PM -0800 X-Subliminal-Message: Use Linux! Use Linux! X-Archive-Number: 200202/86 X-Sequence-Number: 6514 On Thu, Feb 07, 2002 at 01:31:03PM -0800, Stephan Szabo wrote: > On Thu, 7 Feb 2002, Frank Joerdens wrote: > > > Now I want to retrieve rows from A as in > > > > SELECT DISTINCT A.foo > > FROM A,B > > WHERE A.foo [matches some criteria] > > OR B.bar [matches some other criteria] > > AND A.id = B.a_id; > > > > This works fine if there is *at least* one row in B for each row in A. > > If there isn't, I obviously get 0 results even if the column foo in A > > matches the desired criteria. But I do want the rows from A even if > > there is no row in B that is linked to those rows in A that match the > > criteria. If the column bar in B matches the desired criteria, I also > > want the rows in A that are linked to those rows in B. > > Sounds like you want a left outer join. > > Maybe... > select distinct a.foo from > a left outer join b on (a.ID = b.a_id) > where a.foo [matches] or B.bar [matches] Ah! There indeed is an entire little chapter (6.6.3) in my 'Fundamentals of Database Systems' (Elmasri/Navathe) on this, and Bruce talks about it too . . . albeit saying (in my version of his book) that joins ain't supported in 7.0 and that you need to simulate 'em using UNION ALL and subqueries. The problem here was a combination of hubris and laziness on my part: I thought I didn't need to learn about join syntax because I figured it could all be done with boolean logic as in my query example above . . . Thanks! Frank