从多个表MYSQL查找字段

时间:2011-02-18 19:16:55

标签: mysql

我正在尝试为学生显示日程安排。这是地图:

StudentProfile
-studentid

Registration
-registrationid
-studentid

RegistrationSchedule
-regscheduleid(is not a primary key,, is not unique,,can have a lot of instances)
-registrationdid
-scheduleid

Schedules
-scheduleid

我想展示学生的所有日程安排。我真的很沮丧。有人可以做到这一点吗?

3 个答案:

答案 0 :(得分:1)

这可以通过MySQL JOIN Statement

完成
SELECT * FROM StudentProfile sp 
INNER JOIN Registration r ON sp.studentid = r.studentid
INNER JOIN RegistrationSchedule rs ON r.registrationId= rs.registrationId
INNER JOIN Schedules sc ON rs.scheduleid = schedules.scheduleid
WHERE sp.studentid = DESIREDSTUDENTID

这将为ID为DESIREDSTUDENTID的学生选择每个表中的所有列。您可以通过将SELECT *更改为SELECT sc。*

来获取学生日程表中的数据

答案 1 :(得分:0)

似乎以下应该有效,但它仅基于您提供的信息(不多),因此无法保证:

SELECT s.* from Schedules s
join RegistrationSchedule rs on s.scheduleid=rs.scheduleid
join Registration r on rs.registrationid=r.registrationid
join StudentProfile p on r.studentid=p.studentid
WHERE p.studentid=?

您必须在WHERE子句中传递?的值才能获得特定学生的日程安排。

答案 2 :(得分:0)

select * from StudentProfile sp
   inner join Registration r on r.studentid = sp.studentid
   inner join RegistrationSchedule rs on r.registrationid = rs.registrationid
   inner join Schedule s on s.scheduleid = rs.scheduleid