2017-02-23 45 views
2

有一張名爲teachers的表格,其中列有teacher_id,role_codevisit_tutorclass_code列的教師的詳細信息。如果role_code'CT'visit_tutornull,則教師是班級的正式教師。如果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' 
); 

回答

0

這不是如何加入...

嘗試:

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'