Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to Inner Join 3 tables?

Status
Not open for further replies.

csiwa28

Programmer
Apr 12, 2001
177
Is it possible to Inner Join 3 tables? If so, can anyone give me a general format that I could try to follow?
 
here's a quick example:

select * from table1 inner join (table2 inner join table3 on table2.field = table3.field) derivedA on table1.field = derivedA.field

you don't have to fully qualify the names, you can just use aliases and what not like i did

hth
leo

 
derivedA is a derived table... so it comes from the result of (table2 inner join table3 on table2.field = table3.field).


The way SQL works is that it will return tables, and you can use those tables to query from. Here's a shorter example:

select der.field1, der.field2 from (SELECT field1, field2, FROM myTbl) der

this is the same thing as
select field1, field2 from myTbl

sorta following?
the thing with derived tables, is that you can only access the fields that you explicitly call. So let's say there's a field3 that you want to order by:

select der.field1, der.field2 from (SELECT field1, field2, FROM myTbl) der order by der.field3

this doesn't work because you need to include field3 in your subquery... armed with that, let's try this:

select der.field1, der.field2 from (SELECT field1, field2, field3 FROM myTbl) der order by der.field3

that should work.

hth
leo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top