Looking for the correct solution for a generic problem. - Mailing list pgsql-sql

From Frank Joerdens
Subject Looking for the correct solution for a generic problem.
Date
Msg-id 20020207215234.A6766@superfly.archi-me-des.de
Whole thread Raw
Responses Re: Looking for the correct solution for a generic problem.  (Stephan Szabo <sszabo@megazone23.bigpanda.com>)
Re: Looking for the correct solution for a generic problem.  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
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


pgsql-sql by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Nested Transactions
Next
From: Stephan Szabo
Date:
Subject: Re: Looking for the correct solution for a generic problem.