tags:

views:

37

answers:

1

I have some legacy code I'm going through, and i just found a join with two "on" clauses...

select * from table
inner join table3
inner join table2 on table3.key = table2.fkey on table.key = table2.otherkey

What does this kind of join mean? (It currently works so it's not a syntax error)

(Edit: Fixed the code, was missing a join)

+4  A: 

Post your edit it's just a case of knowing the implicit precedence.

select * from table1
inner join table3
inner join table2 on table3.key = table2.fkey on table1.key = table2.otherkey

is the same as

select * from 
table1 inner join 
   (table3 inner join table2 on table3.key = table2.fkey)
on table1.key = table2.otherkey

which hopefully makes more sense. All I have done here is add parentheses.

The first join, table3 to table2, conceptually speaking produces an intermediate table with all the columns from both. table1 is then joined to this using the second on clause you see.

AakashM
Ah, yes... That makes sense, but makes for awful to read code :|
Christian W