我需要一些帮助才能正确创建我的选择...
这是我的桌子:
**teacher** :
id
name
**classroom** :
id
teacher_id
**students** :
id
name
classroom_id
status
我正在尝试选择所有有教室的老师。仅当所有学生的状态 > 10 时才能选择教室...
例如,如果学生的状态为 5,则可能不会选择教室,因此可能不会选择老师(除了他有另一个 OK 教室)
尝试这个 :
select * from teacher t
inner join classroom c on t.id=c.teacher_id
inner join
(select * from students
group by classroom_id having classroom_id not in
(select distinct classroom_id from students where status<=10)) s
on s.classroom_id=c.id
更新:根据您的评论,我认为上面的查询应该有效,但您可以测试此查询:
select * from teacher t where id in
(select distinct teacher_id from classroom where id in
(select distinct classroom_id from students
group by classroom_id having classroom_id not in
(select distinct classroom_id from students where status<=10)
)
)