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

From Jasen Betts
Subject Re: subselect and left join not working?
Date
Msg-id icvsmc$6p1$1@reversiblemaps.ath.cx
Whole thread Raw
In response to subselect and left join not working?  (Jorge Arenas <jorge.arenas@kamarble.com>)
Responses Re: subselect and left join not working?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
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.

to avoid that confusion do it this way:
select zona_id as z from zonas where z not in (select zona_id fromusuarios where per_id =2)

or this way:
select zona_id from zonas where zona_id not in (select usuarios.zona_id fromusuarios where per_id =2)

or this way:
select zona_id from zonas where zona_id not in (select u.zona_id fromusuarios as u  where per_id =2)

-- 
⚂⚃ 100% natural


pgsql-sql by date:

Previous
From: "Stefan Becker"
Date:
Subject: Is there a solution for "SELECT OR INSERT"
Next
From: Tom Lane
Date:
Subject: Re: subselect and left join not working?