2015-03-03 213 views
0

我很困惑如何進行一個SQL查詢,它將顯示學生的詳細信息誰正在採取由特定講師教的主題。問題與MySQL子查詢

有5個表是我的db。

主題(subjectCodesubjectNamecreditHourstudyMode

學生(stdID, stdName`)

講師(lecID, lecName`)

講師學科(lec_subIDlec_userIDsubjectID

學生主題(std_subIDstudent_userIDsubjectCode

+0

添加一些虛擬數據 – 2015-03-03 07:51:22

+0

是和什麼是列什麼是數據?你試過了什麼? – 2015-03-03 07:51:39

+0

我已經使用列名編輯帖子。 – 2015-03-03 08:01:05

回答

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