mysql子查询问题

时间:2015-03-03 07:50:36

标签: mysql sql

我对如何制作一个sql查询感到困惑,该查询将显示正在学习由特定讲师授课的学生的学生详细信息。

有5个表是我的数据库。

主题(subjectCodesubjectNamecreditHourstudyMode

学生(stdID, stdName`)

讲师(lecID, lecName`)

讲师 - 主题(lec_subIDlec_userIDsubjectID

学生主题(std_subIDstudent_userIDsubjectCode

3 个答案:

答案 0 :(得分:1)

您需要应用joinWHERE子句来获取所需的数据。

SELECT DISTINCT 
    Sturent.stdName as StudentName, 
    Subject.subjectName as SubjectName, 
    Lecturer.lecName as LecturerName, 
    Subject.creditHour, 
    Subject.studyMode  
FROM 
  Student JOIN Student-subject 
   ON student.stdID= Student-subject.student_userID
  JOIN Subject 
   ON Student-subject.subjectCode = subject.subjectCode
  JOIN Lecturer-subject 
   ON Lecturer-subject.subjectID = subject.subjectCode
  JOIN Lecturer 
   ON Lecturer-subject.lec_userID = lectrer.lecID
 WHERE
    Lecturer.lecName = 'some lecturer name'

答案 1 :(得分:0)

select Sturent.stdName as StudentName, 
    Subject.subjectName as SubjectName, 
    Lecturer.lecName as LecturerName, 
    Subject.creditHour as CreditHour,
    Subject.studyMode as StudyMode from Student
    join Student-subject on student.stdID= Student-subject.student_userID
    join Subject on Student-subject.subjectCode = subject.subjectCode
    join Lecturer-subject on Lecturer-subject.subjectID = subject.subjectCode
    join Lecturer on Lecturer-subject.lec_userID = lectrer.lecID

select Sturent.stdName as StudentName, 
    Subject.subjectName as SubjectName, 
    Lecturer.lecName as LecturerName, 
    Subject.creditHour as CreditHour,
    Subject.studyMode as StudyMode from Student, Student-subject, 
Subject, Lecturer-subject, Lecturer 
where student.stdID= Student-subject.student_userID
    and Student-subject.subjectCode = subject.subjectCode
    and Lecturer-subject.subjectID = subject.subjectCode
    and Lecturer-subject.lec_userID = lectrer.lecID

答案 2 :(得分:0)

试试这个会起作用:

使用Inner Join

Select t1.`stdId`,t1.`stdName`,t2.`std_subID`,t3.`subjectName`,t3.`creditHour`,t3.`studyMode`,t4.`lec_subID`,t5.`lecName` from Student t1
JOIN Student-subject t2 ON t2.`student_userId`=t1.`stdID`
JOIN Subject t3 ON t3.`subjectCode`=t2.`subjectCode`
JOIN Lecturer-subject t4 ON t4.`subjectID`=t2.`std_subID`
JOIN Lecture t5 ON t5.`lecID`=t4.`lec_userID`