我有一个关于从三个表中选择数据的问题
第一个表是这样的:Timetable

ClassID   teacherID   assistantID
1           1            2
0
2            3            4


`0是休息时间,因此没有助手和老师

Class表:

classID     classname
0            Break
1            MATH
2            ART


teacher

teacherID     teacherName
1               Tom
2               Lily
3               Rose
4               Nile


我想制作一张这样的桌子
Timetables

ClassName      TeacherName     AssistantName
Math            Tom            Liky
Break
Art             Rose            Nile

最佳答案

用不同的别名将teacher表连接两次

select c.className, teach.teacherName, assist.teacherName as assistantName
from Timetable t
join class c on c.classID = t.classID
left join teacher teach on teach.teacherID = t.teacherID
left join teacher assist on assist.teacherID = t.assistantID

关于mysql - 从三个表中选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32010246/

10-12 02:13