pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Frank Joerdens <frank@joerdens.de>
To: pgsql-sql@postgresql.org
Cc: hank@fas-art.com
Cc: annette@fas-art.com
Subject: Looking for the correct solution for a generic problem.
Date: Thu, 7 Feb 2002 21:52:34 +0100
Message-ID: <20020207215234.A6766@superfly.archi-me-des.de> (raw)

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



view thread (5+ messages)  latest in thread

Message-ID: <20020207215234.A6766@superfly.archi-me-des.de>
Permalink:  ../20020207215234.A6766@superfly.archi-me-des.de/
Also on:    postgresql.org/message-id/20020207215234.A6766@superfly.archi-me-des.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: frank@joerdens.de, hank@fas-art.com, annette@fas-art.com
  Subject: Re: Looking for the correct solution for a generic problem.
  In-Reply-To: <20020207215234.A6766@superfly.archi-me-des.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox