2015-03-03 2 views
0

Я смущен тем, как сделать sql-запрос, который будет отображать детали учащихся, которые берут предмет, который преподавал конкретный лектор.Проблема с подзапросом mysql

Существует 5 таблиц - мой дБ.

Тема (subjectCode, subjectName, creditHour, studyMode)

Студент (stdID, stdName`)

преподаватель (lecID, lecName`)

преподаватель-субъект (lec_subID, lec_userID, subjectID)

Студенческая тема (std_subID) , student_userID, subjectCode)

+0

Добавить некоторые фиктивные данные –

+0

Да и каковы столбцы, каковы данные? что ты пробовал? –

+0

Я редактировал сообщение с именем столбца. –

ответ

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

Вы должны применить join и WHERE, чтобы получить нужные данные.

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' 
Смежные вопросы