[英]How to self-join a table with a range of values from both sides?
There is a table called teachers
with details of teachers with columns teacher_id
, role_code
, visit_tutor
and class_code
. 有一个名为表
teachers
与教师的列的细节teacher_id
, role_code
, visit_tutor
和class_code
。 A teacher is a regular teacher of a class if role_code
is 'CT'
and visit_tutor
is null
. 如果
role_code
为'CT'
并且visit_tutor
为null
则老师是该班的普通老师。 He is a visiting teacher of a class if visit_tutor
is not null
. 如果
visit_tutor
不为null
则他是该班的客座老师。
How to get the list of teacher_id
s of teachers who are regular teachers of class with class_code
'AA'
and visiting teacher of class with class_code
'BB'
? 如何获得的名单
teacher_id
老师谁是类的普通教师与第class_code
'AA'
和来访班老师class_code
'BB'
?
The following code is throwing an error because the first subquery is returning multiple rows: 以下代码抛出错误,因为第一个子查询返回多行:
select * from teachers where (
select teacher_id from teachers t1 where t1.role_code='CT' and t1.class_code='AA'
) in (
select teacher_id from teachers t2 where t2.visit_tutor is not null and t2.class_code='BB'
);
That's not how to join... 那不是怎么加入...
Try: 尝试:
select t1.*
from teachers t1
inner join teachers t2
on t1.teacher_id = t2.teacher_id
where t1.role_code='CT' and t1.class_code='AA'
and t2.visit_tutor is not null and t2.class_code='BB'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.