MySQL Multiple Joins 3表(桥接表)

时间:2019-01-29 20:49:55

标签: mysql

第一个表课程列:

courseId|instructorId|desc|courseName|img|

第二个表 StudentCourses 列:

studentCourseId|studentId

第3个表的 UserData 列:

userId|avatar|name

因此,我知道studentId,我正在尝试使用其教师个人资料数据(在本例中为头像和姓名)让学生拥有课程 结果列应为:

 courseId,courseName,desc,img,instructorsname,instructorsavatar

此查询为我提供了课程信息,但不包括教师数据。

SELECT Courses.courseId,Courses.img,Courses.courseName,Courses.`desc` FROM Courses JOIN StudentCourses ON StudentCourses.studentCourseId = Courses.courseId WHERE StudentCourses.studentId ="igkw11tkwa06kpmoe9o6hyytrq0qaqjq"

1 个答案:

答案 0 :(得分:1)

假设讲师ID与用户ID匹配

    SELECT  Courses.courseId
            ,Courses.img
            ,Courses.courseName
            ,Courses.`desc` 
            , u1.name
            , u1.avatar
    FROM Courses 
    INNER JOIN StudentCourses ON StudentCourses.studentCourseId = Courses.courseId 
    INNER JOIN UserData u1 ON u1.userId = Courses.instructorId
    WHERE StudentCourses.studentId ="igkw11tkwa06kpmoe9o6hyytrq0qaqjq"