簡體   English   中英

如何自兩端連接具有一定范圍值的表?

[英]How to self-join a table with a range of values from both sides?

有一個名為表teachers與教師的列的細節teacher_idrole_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'
);

那不是怎么加入...

嘗試:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM