tags:

views:

102

answers:

3

i have seen how to inner join 2 tables where a column is equal to the content in another column. but how do i do this with 7 tables?'

thanks everyone, I figured it out lol after a long time. this seems to work

SELECT *
FROM
  tbl_school
  INNER JOIN tbl_apprequirments ON (tbl_school.schoolname = tbl_apprequirments.schoolname)
  INNER JOIN tbl_citygallery ON (tbl_apprequirments.schoolname = tbl_citygallery.schoolname)
  INNER JOIN tbl_schoolgallery ON (tbl_citygallery.schoolname = tbl_schoolgallery.schoolname)
  INNER JOIN tbl_livingexp ON (tbl_schoolgallery.schoolname = tbl_livingexp.schoolname)
  INNER JOIN tbl_tuition ON (tbl_livingexp.schoolname = tbl_tuition.schoolname)

  where tbl_school.schoolname = 'glendale community college';
+3  A: 
SELECT * FROM t1 JOIN t2 JOIN t3 JOIN t4 JOIN t5
  ON (t2.c=t1.c AND t3.c=t1.c AND t4.c=t1.c AND t5.c=t1.c)

MySQL provides a shorthand for this:

SELECT * FROM t1 JOIN (t2, t3, t4, t5)
  ON (t2.c=t1.c AND t3.c=t1.c AND t4.c=t1.c AND t5.c=t1.c)

This example is for 5 tables. You can repeat as necessary.

See MySQL's join syntax.

Edit: after seeing the clarification from sarmenhb, I think this query also works:

SELECT * FROM t1 JOIN (t2, t3, t4, t5, t6, t7)
  USING (schoolname)
  WHERE t1.schoolname = 'name'
Ayman Hourieh
I count 5, not 7 tables ;)
Stephan202
@Stephan202, I'm sure the OP will get the idea. :)
Ayman Hourieh
select * from tbl_school JOIN (tbl_tuition,tbl_livingexp,tbl_schoolgallery,tbl_apprequirments,tbl_citygallery)USING (schoolname)where tbl_school.`schoolname` = 'glendale community college';
got an error doing that, error is Invalid Token 'where' at position 1 of line 3
@sarmenhb - It works for me. Are you using MySQL or something else? Could you please also try the syntax in the first query?
Ayman Hourieh
A: 

Try something like this...

Select data
from table as tbl1
join as tbl2 on tbl2.data = tbl1.data
join as tbl3 on tbl3.data = tbl1.data

is this what you are looking for?

Eric
+1  A: 

After joining 2 of them, join the thrid to the first two, then the 4th to the first 3, etc,

  Select *
   From T1 Join T2 On  <criteria>
           Join T3, on <criteria>
           Join T4 On <Criteria>
            etc...
Charles Bretana