pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedSQL command join question
5+ messages / 5 participants
[nested] [flat]
* SQL command join question
@ 2006-11-30 00:41 Ehab Galal <ehabgalal123@hotmail.com>
2006-11-30 00:46 ` Re: SQL command join question Phillip Smith <phillips@weatherbeeta.com.au>
2006-11-30 00:57 ` Re: SQL command join question Stephan Szabo <sszabo@megazone.bigpanda.com>
2006-11-30 00:57 ` Re: SQL command join question Richard Broersma Jr <rabroersma@yahoo.com>
2006-11-30 01:02 ` Re: SQL command join question Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 4 replies; 5+ messages in thread
From: Ehab Galal @ 2006-11-30 00:41 UTC (permalink / raw)
To: pgsql-sql
Hi,
I have three tables t1(a, b, c, d), t2(a, b, c, k), and t3(c, e). I need to
outer join them as shown below, but only have all tuples from t1 as output.
But the following syntax does not allow me to do so.
SELECT t1.*
FROM (t1 outer join t2 on (t1.a=t2.a and t1.b=t2.b)) t outer join t3 on
(t1.c=t3.c);
I'm getting the following error message:
ERROR: invalid reference to FROM-clause entry for table "t1"
HINT: There is an entry for table "t1", but it cannot be referenced from
this part of the query.
I'll be grateful if someone may help me with this.
Thanks,
Ehab
_________________________________________________________________
Fixing up the home? Live Search can help
http://imagine-windowslive.com/search/kits/default.aspx?kit=improve&locale=en-US&source=hmem...
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: SQL command join question
2006-11-30 00:41 SQL command join question Ehab Galal <ehabgalal123@hotmail.com>
@ 2006-11-30 00:46 ` Phillip Smith <phillips@weatherbeeta.com.au>
3 siblings, 0 replies; 5+ messages in thread
From: Phillip Smith @ 2006-11-30 00:46 UTC (permalink / raw)
To: 'Ehab Galal' <ehabgalal123@hotmail.com>; pgsql-sql
Would this be more appropriate...?
SELECT t1.*
FROM t1
OUTER JOIN t2 ON (t1.a=t2.a AND t1.b=t2.b)
OUTER JOIN t3 ON (t1.c=t3.c);
-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of Ehab Galal
Sent: Thursday, 30 November 2006 11:42
To: pgsql-sql@postgresql.org
Subject: [SQL] SQL command join question
Hi,
I have three tables t1(a, b, c, d), t2(a, b, c, k), and t3(c, e). I need to
outer join them as shown below, but only have all tuples from t1 as output.
But the following syntax does not allow me to do so.
SELECT t1.*
FROM (t1 outer join t2 on (t1.a=t2.a and t1.b=t2.b)) t outer join t3 on
(t1.c=t3.c);
I'm getting the following error message:
ERROR: invalid reference to FROM-clause entry for table "t1"
HINT: There is an entry for table "t1", but it cannot be referenced from
this part of the query.
I'll be grateful if someone may help me with this.
Thanks,
Ehab
_________________________________________________________________
Fixing up the home? Live Search can help
http://imagine-windowslive.com/search/kits/default.aspx?kit=improve&locale=e
n-US&source=hmemailtaglinenov06&FORM=WLMTAG
---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend
*******************Confidentiality and Privilege Notice*******************
The material contained in this message is privileged and confidential to
the addressee. If you are not the addressee indicated in this message or
responsible for delivery of the message to such person, you may not copy
or deliver this message to anyone, and you should destroy it and kindly
notify the sender by reply email.
Information in this message that does not relate to the official business
of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta.
Weatherbeeta, its employees, contractors or associates shall not be liable
for direct, indirect or consequential loss arising from transmission of this
message or any attachments
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: SQL command join question
2006-11-30 00:41 SQL command join question Ehab Galal <ehabgalal123@hotmail.com>
@ 2006-11-30 00:57 ` Stephan Szabo <sszabo@megazone.bigpanda.com>
3 siblings, 0 replies; 5+ messages in thread
From: Stephan Szabo @ 2006-11-30 00:57 UTC (permalink / raw)
To: Ehab Galal <ehabgalal123@hotmail.com>; +Cc: pgsql-sql
On Wed, 29 Nov 2006, Ehab Galal wrote:
> I have three tables t1(a, b, c, d), t2(a, b, c, k), and t3(c, e). I need to
> outer join them as shown below, but only have all tuples from t1 as output.
> But the following syntax does not allow me to do so.
>
> SELECT t1.*
> FROM (t1 outer join t2 on (t1.a=t2.a and t1.b=t2.b)) t outer join t3 on
> (t1.c=t3.c);
I think you don't want to alias the output of the t1/t2 join to t if
you're planning to continue referring to t1 in the rest of the query since
I think the alias is going to hide the original t1 name.
I'm not sure which outer join you were trying to use, but assuming left
for now, I think something like
SELECT t1.* FROM t1 left outer join t2 on (t1.a=t2.a and t1.b=t2.b
) left outer join t3 on (t1.c=t3.c);
might work for you.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: SQL command join question
2006-11-30 00:41 SQL command join question Ehab Galal <ehabgalal123@hotmail.com>
@ 2006-11-30 00:57 ` Richard Broersma Jr <rabroersma@yahoo.com>
3 siblings, 0 replies; 5+ messages in thread
From: Richard Broersma Jr @ 2006-11-30 00:57 UTC (permalink / raw)
To: Ehab Galal <ehabgalal123@hotmail.com>; pgsql-sql
> SELECT t1.*
> FROM (t1 outer join t2 on (t1.a=t2.a and t1.b=t2.b)) t outer join t3 on
^^
> (t1.c=t3.c);
^^ -- which one t or t1?
> I'm getting the following error message:
> ERROR: invalid reference to FROM-clause entry for table "t1"
> HINT: There is an entry for table "t1", but it cannot be referenced from
> this part of the query.
I am not use if I am about to give to the correct advice, but here is what stands out at me:
when you specify an outer join, I believe that you have to specify whether it is a
(left|right|full).
Also you are using a "t" to alias the (t1 .. t2 on (...)). First of all, I am not sure this is
good syntax, but if I am wrong I will have learned something new. That aside, if it is legal
syntax, I don't believe that you can refer to any of it's enternal tables any more. So (t1.c=...)
should really be (t.c=...).
this is what I expect would work:
select
*
from
t1
left outer join
t2
on
((t1.a,t1.b) = (t2.a,t2.b))
left outer join
t3
on
(t1.c = t3.c)
;
Regards,
Richard Broersma Jr.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: SQL command join question
2006-11-30 00:41 SQL command join question Ehab Galal <ehabgalal123@hotmail.com>
@ 2006-11-30 01:02 ` Tom Lane <tgl@sss.pgh.pa.us>
3 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2006-11-30 01:02 UTC (permalink / raw)
To: Ehab Galal <ehabgalal123@hotmail.com>; +Cc: pgsql-sql
"Ehab Galal" <ehabgalal123@hotmail.com> writes:
> SELECT t1.*
> FROM (t1 outer join t2 on (t1.a=t2.a and t1.b=t2.b)) t outer join t3 on
> (t1.c=t3.c);
> ERROR: invalid reference to FROM-clause entry for table "t1"
> HINT: There is an entry for table "t1", but it cannot be referenced from
> this part of the query.
Drop the alias on the outer join (the "t"). Per SQL spec, that masks
table names (and aliases) within the join from the rest of the query.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2006-11-30 01:02 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-11-30 00:41 SQL command join question Ehab Galal <ehabgalal123@hotmail.com>
2006-11-30 00:46 ` Phillip Smith <phillips@weatherbeeta.com.au>
2006-11-30 00:57 ` Stephan Szabo <sszabo@megazone.bigpanda.com>
2006-11-30 00:57 ` Richard Broersma Jr <rabroersma@yahoo.com>
2006-11-30 01:02 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox