MySQL中的多表SQL连接

时间:2011-03-05 09:49:08

标签: mysql

如何在一个查询中连接多个表 这些是我的表

registration
  registrationid
  regschedid
  studentid

registrationschedule
  regschedid

session
  sessionid
  regschedid
  sessiondate

schedules
  scheduleid
  regschedid
  teacherid

faculty
  teacherid
  fname

我想加入所有这些,以便我可以获得fname和会话日期,请帮助我..

顺便说一句,我必须指定registration.studentid才能得到实际的学生

3 个答案:

答案 0 :(得分:3)

要连接多个表,您可以使用与连接2个表相同的技术:

SELECT *
FROM registration
JOIN registrationschedule ON registration.regschedid = regschedid
JOIN student ON registration.studentid = student.studentid
--- etc

答案 1 :(得分:0)

 SELECT registration.*,registrationschedule.*, session.*,schedules.*,faculty.*
 FROM registration
 LEFT JOIN registrationschedule on registration.regschedid = registrationschedule.regschedid
 LEFT JOIN session on session.regschedid = registrationschedule.regschedid
 LEFT JOIN schedules on schedules.regschedid = session.regschedid
 LEFT JOIN faculty on faculty.teacherid = schedules.teacherid
 WHERE registrationid = 1;

答案 2 :(得分:0)

SELECT * FROM registration 
JOIN registrationschedule ON registration.regschedid=regschedid 
JOIN session ON registration.regschedid=session.regschedid 
JOIN schedules ON regschedid.regschedid=regschedid
JOIN faculty ON schedules.teacherid = faculty.techerid
WHERE student.id = ?