tags:

views:

25

answers:

2

I wrote this, and it is wrong syntax, help me fix it, I want 'T' to be an alias of the result of the two inner joins.

select T.id 
from table1 
  inner join table2 on table1.x = table2.y  
  inner join table3 on table3.z = table1.w as T;
+4  A: 

You can't directly name the result of a join. One option is to use a subquery:

select T.id
from (
  select *
  from table1
  inner join table2 on table1.x = table2.y
  inner join table3 on table3.z = table1.w
) T

Another option is subquery factoring:

with T as (
  select *
  from table1
  inner join table2 on table1.x = table2.y
  inner join table3 on table3.z = table1.w
)
select T.id
from T
David Phillips
+2  A: 

You cannot use aliases to name the "entire" join, you can, however, put aliases on individual tables of the join:

select t1.id
from table1 t1
   inner join table2 t2 on t1.x = t2.y
   inner join table3 t3 on t3.z = t1.w

In the projection, you will have to use the alias of the table, which defines the id column you are going to select.

Dirk