我有下面的拖表表1:教师表
teacher_id teacher_name
1 xx
2 yy
3 zz
表 2:学生表
stu_id stu_name tearcher1_id teacher2_id tearcher3_id
1 aa 1 2
2 bb 2 3
3 cc 1
我想从老师那里得到一个 sql 语句的列表,包括出现在学生表中的人数,如下所示:
teacher_id teacher_name num_selected_by_stu
1 xx 2
2 yy 2
3 zz 1
我在sql下面试过但似乎不起作用,
select * from teatcher t1
left join (
select stu_id,tearcher1_id,tearcher2_id,tearcher3_id,count(stu_id) as num_selected_by_stu from student
group by stu_id,tearcher1_id,tearcher2_id,tearcher3_id) t2
ON ( t2.teacher1_id=t1.teacher_id or t2.teacher2_id=t1.teacher_id or t2.teacher3_id=t1.teacher_id)
而且,有人可以帮忙吗?