[英]How to self-join a table with a range of values from both sides?
有一個名為表teachers
與教師的列的細節teacher_id
, role_code
, visit_tutor
和class_code
。 如果role_code
為'CT'
並且visit_tutor
為null
則老師是該班的普通老師。 如果visit_tutor
不為null
則他是該班的客座老師。
如何獲得的名單teacher_id
老師誰是類的普通教師與第class_code
'AA'
和來訪班老師class_code
'BB'
?
以下代碼拋出錯誤,因為第一個子查詢返回多行:
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'
);
那不是怎么加入...
嘗試:
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.