使用外键加入表

时间:2016-07-04 09:31:05

标签: php mysql

学生表:

(student_id(PK),student_name,subject_enrolled(FK))

教师表:

(teacher_id(PK),teacher_name,subject_teaches(FK))

主题表:

(subject_id(PK),subject_name)

如何为此设置正确的JOIN TABLES查询:

Student_id,teacher_name- multiple names 
(resulted from same subject_enrolled and subject_teaches)

谢谢你们!

2 个答案:

答案 0 :(得分:0)

SELECT * FROM students AS st
LEFT JOIN subjects AS su ON su.subject_id = st.subject_enrolled
LEFT JOIN teachers AS t ON su.subject_id = t.subject_teaches

答案 1 :(得分:0)

试试这个

Select students.student_id,GROUP_CONCAT(teachers.teacher_name)
FROM students
JOIN teachers ON teachers.subject_teaches = students.subject_enrolled
GROUP BY students.student_id

SQL Fiddle Demo