我想问我要在查询中添加什么来选择只教一门课的类(class)。我的 table 是:
Teacher Course
ID Name Course_ID Teacher_ID
1 George 1 1
2 Sam 2 2
3 Julie 3 1
我想获取只教过一个类的老师ID。我有:
select teacher.id
from teacher, course
where teacher.id = course.teacher_id;
我正在考虑添加
having (count(course.teacher_id)) = 1
或
where count(t) = (select count(*) from course) and t = 0
但我收到“无效使用组错误”。如何更改它来修复我的查询?
最佳答案
你已经尝试过的差不多了,只需添加group by
,并使用join
而不是,
来合并表格:
select teacher.id
from teacher
join course
on teacher.id = course.teacher_id
group by teacher.id
having (count(course.Course_ID)) = 1 -- note count(Course_ID), not count(teacher_id)
关于mysql - 寻找只教一个类的老师,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42501214/