Re: Help with strange join

From: Alban Hertroys <alban(at)magproductions(dot)nl>
To: Victor Spång Arthursson <victor(at)tosti(dot)dk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Help with strange join
Date: 2005-02-04 12:51:18
Message-ID: 42036FC6.8000106@magproductions.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Victor Spång Arthursson wrote:
> The tables are link according to the following:
>
> receipts <- related_ingredients <- ingredients <- languages

> If I just do JOINs, I will not be able to find out if only one or all of
> the ingredients are translated. What I need is something that, for
> example, returns the receiptnumber if, and only if, the number of
> translated ingredients that is returned (after joining ingredients with
> languages) is the same as the number of entrys in the table
> "related_ingredients".

Something like this could work (column names and values made up while
going on):

SELECT *
FROM (receipts INNER JOIN related_ingredients ON (receipt_id)) r
OUTER JOIN (ingredients INNER JOIN languages ON (ingredient_id)) i
WHERE i.language_id = 1
AND i.name IS NOT NULL

The idea is that the outer join allows receipts with untranslated
ingredients to end up in the results as NULL values, so that you can
check for them.

I suppose a HAVING clause would also work, but AFAIK you need to GROUP
BY to be able to do that...

All of this is untested of course, so I may have made mistakes.

--
Alban Hertroys
MAG Productions

T: +31(0)53 4346874
F: +31(0)53 4346876
E: alban(at)magproductions(dot)nl
W: http://www.magproductions.nl

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joolz 2005-02-04 12:56:23 Re: error-tolerant COPY FROM
Previous Message Mike Rylander 2005-02-04 12:49:46 Re: error-tolerant COPY FROM