Re: Looking for the correct solution for a generic problem.

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Frank Joerdens <frank(at)joerdens(dot)de>
Cc: <pgsql-sql(at)postgresql(dot)org>, <hank(at)fas-art(dot)com>, <annette(at)fas-art(dot)com>
Subject: Re: Looking for the correct solution for a generic problem.
Date: 2002-02-07 21:31:03
Message-ID: 20020207132603.D32753-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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]

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2002-02-07 21:50:53 Re: Looking for the correct solution for a generic problem.
Previous Message Mark Nielsen 2002-02-07 21:02:16 Re: Help with a SQL query