如何连接7个表,其中列(存在于所有表中)等于所有表中的相同内容?

时间:2009-05-20 21:43:57

标签: php mysql

我已经看到了如何内连接2个表,其中列等于另一列中的内容。但我如何用7个表做这个?'

谢谢大家, 经过很长一段时间,我想出了lol。这似乎有效

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 个答案:

答案 0 :(得分:3)

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提供了一个简写:

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)

此示例适用于5个表。您可以根据需要重复。

参见MySQL的join syntax

编辑:看到sarmenhb的澄清后,我认为这个查询也有效:

SELECT * FROM t1 JOIN (t2, t3, t4, t5, t6, t7)
  USING (schoolname)
  WHERE t1.schoolname = 'name'

答案 1 :(得分:1)

加入其中2个后,加入第三个到前两个,然后是第4个到第3个等,

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

答案 2 :(得分:0)

尝试这样的事情......

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

这就是你要找的东西吗?