【发布时间】:2021-08-28 17:44:37
【问题描述】:
假设我有 3 个这样的表:
class
id
capacity
student
id
student_class
id_student
id_class
SELECT c.id
FROM student_class sc
JOIN class c
ON c.id=sc.id_class
JOIN student s
ON s.id=sc.id_student
HAVING MAX((SELECT(COUNT(sc2.id) FROM student_class sc2 WHERE sc2.id_student=s.id AND sc2.id_course=c.id)/c.capacity))
我想找到注册/容量比率最高的课程。
我应该在聚合函数中使用 select 子句来编写像这样的 having 子句吗?
HAVING MAX((query that gives me the total number of students enrolled in a course)/course capacity))
或者有更好的方法吗?
【问题讨论】:
-
请提供样本数据和期望的结果。你的问题没有任何意义,除非
MAX()可以返回NULL或0。 -
@GordonLinoff 我编辑提供示例数据