Re: subselect and left join not working?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: subselect and left join not working?
Дата
Msg-id 25301.1291044420@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: subselect and left join not working?  (Jasen Betts <jasen@xnet.co.nz>)
Ответы Re: subselect and left join not working?
Список 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 по дате отправления:

Предыдущее
От: Jasen Betts
Дата:
Сообщение: Re: subselect and left join not working?
Следующее
От: Jorge Arenas
Дата:
Сообщение: Re: subselect and left join not working?