Re: subselect and left join not working?

From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: subselect and left join not working?
Date: 2010-11-29 09:44:44
Message-ID: icvsmc$6p1$1@reversiblemaps.ath.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 2010-11-29, Jorge Arenas <jorge(dot)arenas(at)kamarble(dot)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.

to avoid that confusion do it this way:

select zona_id as z from zonas where z not in (select zona_id from
usuarios where per_id =2)

or this way:

select zona_id from zonas where zona_id not in (select usuarios.zona_id from
usuarios where per_id =2)

or this way:

select zona_id from zonas where zona_id not in (select u.zona_id from
usuarios as u where per_id =2)

--
⚂⚃ 100% natural

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2010-11-29 15:27:00 Re: subselect and left join not working?
Previous Message Jorge Arenas 2010-11-29 05:38:48 subselect and left join not working?