内部连接SQL

时间:2018-03-21 02:44:57

标签: php mysql sql inner-join

Learning_tutors:
Learning_tutors table

Learning_subject:
Learning_subject table

Learning_subjects:
Learning_subjects table

Learning_level:
Learning_level table

我希望能够根据学习科目表中的subjectID获取教导科目的导师的名字,并将其加入学习科目表,该表格对应于学习中的tutorID导师表

这是我尝试过的

SELECT learning_subjects.levelid, 
       learning_subjects.tutorid, 
       learning_tutor.id, 
       learning_tutor.NAME, 
       learning_level.id 
FROM   learning_subjects 
       INNER JOIN learning_level 
               ON learning_subjects.levelid = learning_level.id 
       INNER JOIN learning_subjects.tutorid 
               ON learning_tutor.id 
WHERE  learning_subjects.tutorid = 1 

3 个答案:

答案 0 :(得分:-1)

select
    s.levelID, t.Name, l.Subject 
from
    Learning_tutors t, Learning_subject l, Learning_subjects s 
where s.tutorID = t.ID AND s.subjectID = l.ID

这为您提供了一种从各种表格中获取信息的灵活方式。

答案 1 :(得分:-1)

我认为你的第二次加入稍微偏离了

SELECT learning_subjects.levelid, 
       learning_subjects.tutorid, 
       learning_tutor.id, 
       learning_tutor.NAME, 
       learning_level.id 
FROM   learning_subjects 
       INNER JOIN learning_level 
               ON learning_subjects.levelid = learning_level.id 
       **INNER JOIN learning_tutors
               ON learning_tutor.id = learning_subjects.tutorid**
WHERE  learning_subjects.tutorid = 1 

答案 2 :(得分:-1)

您需要在 INNER JOIN WHERE 子句部分略有变化:

 FROM learning_subjects INNER JOIN learning_level ON learning_subjects.levelid = learning_level.id 
                        INNER JOIN learning_tutor ON learning_subjects.tutorid = learning_tutor.id
 WHERE learning_subjects.subjectID = 1