Looking for the correct solution for a generic problem.

From: Frank Joerdens <frank(at)joerdens(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Cc: hank(at)fas-art(dot)com, annette(at)fas-art(dot)com
Subject: Looking for the correct solution for a generic problem.
Date: 2002-02-07 20:52:34
Message-ID: 20020207215234.A6766@superfly.archi-me-des.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Mark Nielsen 2002-02-07 20:53:02 Re: Looking for the correct solution for a generic problem.
Previous Message Mark Nielsen 2002-02-07 20:43:40 How do you return more than one variable in a plpgsql function?