我已经看到了如何内部联接2个表,其中一列等于另一列中的内容。但是我该如何用7张桌子呢?

感谢大家,
我想了很久以后。这似乎有效

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';

最佳答案

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'

10-06 14:38