我需要帮助来查询一个查询,该查询会拉出一个具有相同条目的条目。 教师可以选择任何科目进行教学,学生可以选择任何科目进行教学。 需要找一位选择教授同一学科组合的教师。
Table Student
id name
1 x
2 y
Table student_Subjects
id subject_id student_id
1 1 1
2 2 1
3 1 2
Teacher
id name
1 tx
2 ty
Table teacher_subjects
id subject_id teacher_id
1 1 2
2 2 2
3 1 1
Subject
id name
1 English
2 Maths
现在需要找一位选择与学生x教授相同科目的老师。
答案 0 :(得分:0)
试试这个
SELECT GROUP_CONCAT(subject_id ORDER BY subject_id) as teacher_concat ,teacher_id
FROM teacher_subjects
GROUP BY teacher_id
HAVING teacher_concat IN
(SELECT group_concat(subject_id ORDER BY subject_id)
FROM student_subjects GROUP BY student_id)