我有三张桌子,我想要一个查询选择教师姓名和每位老师保留的课程数量。
老师:
| idt | name |
类:
| idc | name |
储备:
| idc | idt |
我的查询:
select
t.name, count(distinct(r.idc))
from
teacher t
join
reserve r
on
r.idt = t.idt
join
class c
on
c.idc = r.idc
group by r.idc
当我运行此操作时,我收到以下错误:not a group by expression.
答案 0 :(得分:2)
group by
子句需要包含select
语句中的所有非聚合列;在你的情况下它应该是t.name
。此外,distinct
不是一个函数,而是一个关键字,不应该有括号。
select
t.name,
count(distinct r.idc) as number_of_classes
from
teacher t
join
reserve r on r.idt = t.idt
join
class c on c.idc = r.idc
group by
t.name