From: Stephan Szabo <sszabo@megazone.bigpanda.com>
To: Steve SAUTETNER <steve@sautetner.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Problem with "NOT IN (subquery)
Date: Sun, 13 Nov 2005 01:16:18 -0800 (PST)
Message-ID: <20051113010417.D61269@megazone.bigpanda.com> (raw)
In-Reply-To: <PCEBIJLLLHHPALLADENFGECICDAA.steve@sautetner.com>
References: <PCEBIJLLLHHPALLADENFGECICDAA.steve@sautetner.com>
On Sun, 13 Nov 2005, Steve SAUTETNER wrote:
> Hi,
>
> I have a table named "famille" whose structure and content is :
>
> famille_code | famille_mere_famille_code | famille_libelle |
> famille_niveau
> --------------+---------------------------+-------------------+-------------
> ---
> 000000 | | Mhre |
Is that a NULL famille_mere_famille_code?
> The first col is the family id and the second is the mother family id.
> I would get a list of all families that are never in the col n°2, so
> the families that aren't node but leaf.
>
> The query, i made was "SELECT * FROM famille WHERE famille_code NOT IN
> (SELECT DISTINCT famille_mere_famille_code FROM famille);"
>
> But the DB returns 0 records instead of 15. If i use a list instead of a
> subquery it works normaly but it's not easy to manage it like this.
>
> So if anyone can help me please ...
In the case where the subselect returns a NULL, the behavior of IN and NOT
IN is rather unfortunate. A NOT IN B is basically NOT(A IN B) and A IN B
is basically A =ANY B IIRC. However, A=ANY B only returns false if A = Bi
returns false for all Bi contained in B and A = NULL returns unknown, not
false, so NOT IN cannot return true if the subselect contains a NULL.
If that is a null above, probably the best solution is to exclude NULLs
from the subselect results.
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: sszabo@megazone.bigpanda.com, steve@sautetner.com
Subject: Re: Problem with "NOT IN (subquery)
In-Reply-To: <20051113010417.D61269@megazone.bigpanda.com>
* 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