Re: how to restrict inner results in OUTER JOIN?

From: "Nigel J(dot) Andrews" <nandrews(at)investsystems(dot)co(dot)uk>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: Drew Wilson <amw(at)speakeasy(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: how to restrict inner results in OUTER JOIN?
Date: 2003-05-06 06:40:40
Message-ID: Pine.LNX.4.21.0305060737520.10245-100000@ponder.fairway2k.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 6 May 2003, Martijn van Oosterhout wrote:

> On Mon, May 05, 2003 at 09:40:10PM -0700, Drew Wilson wrote:
> > Now, I would like to exclude all rows whose group_id is NOT 1, but
> > include the rows whose group_id is NULL.
> >
> > I thought adding a WHERE clause would get me what I want...
> > SELECT * FROM foo f LEFT OUTER JOIN secure_group sg
> > ON (f.group_id = sg.group_id) WHERE sg.group_id = 1;
>
> How about:
>
> SELECT * FROM foo f LEFT OUTER JOIN secure_group sg
> ON (f.group_id = sg.group_id)
> WHERE ( sg.group_id = 1 or sg.group_id IS NULL );
>
> Hope this helps,

Except you made the same typo mistake in the original question, which was to
miss out the NOT part of the "group_id is NOT 1" condition.

So...
SELECT * FROM foo f LEFT OUTER JOIN secure_group sg
ON (f.group_id = sg.group_id)
WHERE ( sg.group_id <> 1 or sg.group_id IS NULL );

--
Nigel J. Andrews

In response to

Browse pgsql-general by date

  From Date Subject
Next Message shoaib 2003-05-06 06:46:43 Re: Database server restarting
Previous Message Martijn van Oosterhout 2003-05-06 06:40:01 Re: Database server restarting