我有一个表,其中包含表之间的某些关系?
school_table的school_id
class_table的class_id
teacher_table的老师ID
主题表的主题ID
我想通过替换子表中每个ID的名称来检索整个表。 例如在检索表为表或视图时,我想查看每个学校名称代替school_id,班级名称代替class_id,以及其他班级和科目以及表中的所有行(如上图所示)>
上面显示的图像是我存储在mysql数据库中的原始表。我想要这个作为结果:
assign_subject_id | Schoolname | teachername | classname | subjectName | session
1 | UCDC | rahul | one | math | 2018-19
2 | UCDC | gopi | two | CS | 2018-19
school_table:
school_id | schoolname
7 | UCDC
以此类推。
谢谢。
答案 0 :(得分:0)
假设您有school_table,teacher_table,class_table,subject_table
每个模式都与school_table(school_id |学校名称)
您需要加入
INNER JOIN(如果所有关系键必须匹配)
select a.assign_subject_id, b.schoolname, c.teachername
, d.classname, e.subjectName, a.session
from my_table a
inner join school_table b on a.school_id = b.school_id
inner join teacher_table c on a.teacher_id = c.teacher_id
inner join class_table d on a.class_id = d.class_id
inner join subject_table e on a.subject_id = e.subject_id
如果关系键不匹配,请左键
select a.assign_subject_id, b.schoolname, c.teachername
, d.classname, e.subjectName, a.session
from my_table a
left join school_table b on a.school_id = b.school_id
left join teacher_table c on a.teacher_id = c.teacher_id
left join class_table d on a.class_id = d.class_id
left join subject_table e on a.subject_id = e.subject_id