Received: from superfly.archi-me-des.de ([62.8.212.177]) by postgresql.org (8.11.3/8.11.4) with ESMTP id g17KsBE14794 for ; Thu, 7 Feb 2002 15:55:02 -0500 (EST) (envelope-from frank@joerdens.de) Received: (from frank@localhost) by superfly.archi-me-des.de (8.9.3/8.9.3) id VAA06910; Thu, 7 Feb 2002 21:52:34 +0100 Date: Thu, 7 Feb 2002 21:52:34 +0100 From: Frank Joerdens To: pgsql-sql@postgresql.org Cc: hank@fas-art.com, annette@fas-art.com Subject: Looking for the correct solution for a generic problem. Message-ID: <20020207215234.A6766@superfly.archi-me-des.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii User-Agent: Mutt/1.1.2i X-Subliminal-Message: Use Linux! Use Linux! X-Archive-Number: 200202/83 X-Sequence-Number: 6512 I've been wondering about this for quite a while now. And I suspect there is a bog-standard way that is better than the rather clumsy approach I am following now (it feels clumsy anyway). I've got table B which is linked to table A as in CREATE TABLE A ( id serial, foo text, ); CREATE TABLE B ( a_id int references A (id), id serial, bar text ); 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. The solution I am using now is to create a 'dummy' row in B for each row in A on the application level, which I then filter out of the result sets again (B gets an extra 'dummy' column which is set to true, if applicable). I not only suspect that there is a proper way to do this, but that there is ample discussion in the relevant literature . . . if I only knew under which keyword to look it up . . . Regards, Frank