Re: subselect and left join not working? - Mailing list pgsql-sql

From Tom Lane
Subject Re: subselect and left join not working?
Date
Msg-id 25301.1291044420@sss.pgh.pa.us
Whole thread Raw
In response to Re: subselect and left join not working?  (Jasen Betts <jasen@xnet.co.nz>)
Responses Re: subselect and left join not working?
List pgsql-sql
Jasen Betts <jasen@xnet.co.nz> writes:
> On 2010-11-29, Jorge Arenas <jorge.arenas@kamarble.com> wrote:
>> select zona_id from zonas where zona_id not in (select zona_id from usuarios 
>          #######                  #######                #######
>> where per_id =2)

>  select 'FRED' from from usuarios where per_id =2

> what'shappening is your not in subquery is being 'corrupted' by the
> surrounding query, the expression zona_id is being replaced with the 
> value from the main query. so the inner query return multiple copies
> of the value from the outer query and the not-in fails.

That explanation is nonsense, and so is the proposed fix.

What I suspect is really going on is that the subselect yields one or
more NULL values.  If there's a NULL then NOT IN can never return TRUE,
only FALSE (if the tested value is definitely present) or NULL (meaning
it might match one of the NULLs, because NULL means "unknown" in this
context).  Newbies get caught by that all the time :-( ... it's not one
of SQL's better features.
        regards, tom lane


pgsql-sql by date:

Previous
From: Jasen Betts
Date:
Subject: Re: subselect and left join not working?
Next
From: Jorge Arenas
Date:
Subject: Re: subselect and left join not working?