subselect and left join not working?

Поиск
Список
Период
Сортировка
Искать

subselect and left join not working?

От:
Jorge Arenas <jorge.arenas@kamarble.com>
Дата:
Hi,

I am using postgres 8.4 and I am having problems doing a very simple query:

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

but I get no records in return.

The problem is that, if I I break down the quey, I get:

select zona_id from zonas

"A"
"B"
"C"
"D"
"E"
"F"
"FCHIH"
"FCOAH"
"DGO"
"T"
"FGTO"
"FGRO"
"FQRO"
"FHGO"
"JAL"
"MOR"
"FPUE"
"FQROO"
"FSLP"
"F VER"
"FYUC"
""

select zona_id from usuarios where per_id =2

"E"
"F"
"FCHIH"
"JAL"
"FPUE"
"F VER"
"C"
"D"
"A"
""

but with

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

I dont' get records when I am expecting to see this:

"B"
"FCOAH"
"DGO"
"T"
"FGTO"
"FGRO"
"FQRO"
"FHGO"
"MOR"
"FQROO"
"FSLP"
"FYUC"
""

What's wrong? Please help. Any clues?


---
It also does not work a left join on 'zonas'. Some 'zonas' are designated to some users but in my case is not wroking:

t1
usr    zone
1       A
2       C
3       D

t2
zone
A
B
C
D
E

I want to see
zone    usr
A        1
B
C        2
D        3
E

Re: subselect and left join not working?

От:
Jasen Betts <jasen@xnet.co.nz>
Дата:
On 2010-11-29, Jorge Arenas  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

Re: subselect and left join not working?

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Jasen Betts  writes:
> On 2010-11-29, Jorge Arenas  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

Re: subselect and left join not working?

От:
Jorge Arenas <jorge.arenas@kamarble.com>
Дата:
Tom,

The subselect worked when I removed nulls. Thanks! Now I am facing a similar problem with the left join:

select zonas.zona_id from zonas order by zona_id
"A"
"B"
"C"
"D"
"DGO"
"E"
"F"
"F VER"
"FCOAH"
"FCHIH"
"FGRO"
"FGTO"
"FHGO"
"FPUE"
"FQRO"
"FQROO"
"FSLP"
"FYUC"
"JAL"
"MOR"
"T"
"x"


select zona_id, usr_folio from  usuarios where per_id = 2 order by usuarios.zona_id

"A"     1002
"C"     1003
"D"     1004
"E"     1005
"F"     1006
"F VER" 1010
"FCHIH" 1007
"FPUE"  1009
"JAL"   1008
"x"     1000

select zonas.zona_id, usr_folio from zonas left join usuarios on zonas.zona_id = usuarios.zona_id where per_id = 2 order by zonas.zona_id

"A"     1002
"C"     1003
"D"     1004
"E"     1005
"F"     1006
"F VER" 1010
"FCHIH" 1007
"FPUE"  1009
"JAL"   1008
"x"     1000

but I am expecting those zonas that have no usr_folio assignated

"A"     1002
"B"
"C"     1003
"D"     1004
"DGO"
"E"     1005
"F"     1006
"F VER" 1010
"FCOAH"
"FCHIH" 1007
"FGRO"
"FGTO"
"FHGO"
"FPUE"  1009
"FQRO"
"FQROO"
"FSLP"
"FYUC"
"JAL"   1008
"MOR"
"T"
"x"     1000

which can be achieve by doing:

select zona_id, usr_folio from  usuarios where per_id = 2
union
select zona_id,null from zonas where zona_id not in (select zona_id from  usuarios where per_id = 2)
order by zona_id

but I would like to use the left join instead

thanks for your help

Jorge.















On Mon, Nov 29, 2010 at 9:27 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> 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

Re: subselect and left join not working?

От:
Carla <cgourofino@hotmail.com>
Дата:
Try it:

select zonas.zona_id, usr_folio from zonas left join usuarios on (per_id = 2 and zonas.zona_id = usuarios.zona_id) order by zonas.zona_id;

Or:

select zonas.zona_id, usr_folio from zonas left join usuarios on zonas.zona_id = usuarios.zona_id where per_id = 2 or usuarios.zona_id is null order by zonas.zona_id;

When you do a left join and a left table value does not match any value of the right table, the left table's column will have a value and all of the right table' columns will have NULL (inclusive per_id).


Carla O.

2010/11/30 Jorge Arenas <jorge.arenas@kamarble.com>
Tom,

The subselect worked when I removed nulls. Thanks! Now I am facing a similar problem with the left join:

select zonas.zona_id from zonas order by zona_id
"A"
"B"
"C"
"D"
"DGO"
"E"
"F"
"F VER"
"FCOAH"
"FCHIH"
"FGRO"
"FGTO"
"FHGO"
"FPUE"
"FQRO"
"FQROO"
"FSLP"
"FYUC"
"JAL"
"MOR"
"T"
"x"


select zona_id, usr_folio from  usuarios where per_id = 2 order by usuarios.zona_id

"A"     1002
"C"     1003
"D"     1004
"E"     1005
"F"     1006
"F VER" 1010
"FCHIH" 1007
"FPUE"  1009
"JAL"   1008
"x"     1000

select zonas.zona_id, usr_folio from zonas left join usuarios on zonas.zona_id = usuarios.zona_id where per_id = 2 order by zonas.zona_id

"A"     1002
"C"     1003
"D"     1004
"E"     1005
"F"     1006
"F VER" 1010
"FCHIH" 1007
"FPUE"  1009
"JAL"   1008
"x"     1000

but I am expecting those zonas that have no usr_folio assignated

"A"     1002
"B"
"C"     1003
"D"     1004
"DGO"
"E"     1005
"F"     1006
"F VER" 1010
"FCOAH"
"FCHIH" 1007
"FGRO"
"FGTO"
"FHGO"
"FPUE"  1009
"FQRO"
"FQROO"
"FSLP"
"FYUC"
"JAL"   1008
"MOR"
"T"
"x"     1000

which can be achieve by doing:

select zona_id, usr_folio from  usuarios where per_id = 2
union
select zona_id,null from zonas where zona_id not in (select zona_id from  usuarios where per_id = 2)
order by zona_id

but I would like to use the left join instead

thanks for your help

Jorge.















On Mon, Nov 29, 2010 at 9:27 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> 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


FAQ