[英]how can i find this query in mysql workbench
For each student, find the number of courses they take and sort the rows in descending order. 对于每个学生,找到他们所修课程的数量,并按降序对行进行排序。 (eg student id, the number of courses taken by that student) (例如,学生证,该学生参加的课程数量)
STUDENT TABLE
| ID | name | dept_name | tot_cred |
| S0901 | Alice | Comp.Sci. | 83 |
| S0902 | Martha | Comp.Sci. | 75 |
| S0903 | Micheal | Comp.Sci. | 45 |
| S0904 | Rose | Comp.Sci. | 77 |
| S0905 | Alfie | Comp.Sci. | 91 |
| S1901 | Brad | Biology | 23 |
TAKES TABLE
| ID | course_id | sec_id | semester | year | grade
| S0901 | CS-101 | 1 | Fall | 2009 | A
| S0901 | CS-315 | 1 | Spring | 2010 | B+
| S0901 | HIS-351 | 1 | Spring | 2010 | A-
| S0901 | MTH-101 | 1 | Fall | 2009 | A
| S0901 | MTH-102 | 1 | Spring | 2009 | B+
| S0902 | CS-101 | 1 | Fall | 2009 | A
| S0902 | CS-315 | 1 | Spring | 2010 | B+
| S0902 | CS-319 | 1 | Spring | 2010 | B
| S0902 | HIS-351 | 1 | Spring | 2010 | A-
| S0902 | MTH-101 | 1 | Fall | 2009 | A
| S0902 | MTH-102 | 1 | Spring | 2009 | B+
| S1901 | CS-101 | 1 | Fall | 2009 | B+
| S1901 | CS-190 | 1 | Spring | 2009 | C
| S1901 | CS-315 | 1 | Spring | 2010 | A-
| S1901 | HIS-351 | 1 | Spring | 2010 | A-
Technically, @Marcinek's answer may not be sufficient because it omits students who take zero classes. 从技术上讲,@ Marcinek的答案可能不够,因为它忽略了参加零班学习的学生。 I would use this instead: 我会改用这个:
SELECT STUDENT.ID, COUNT(TAKES.ID)
FROM STUDENT LEFT JOIN TAKES ON STUDENT.ID = TAKES.ID
GROUP BY STUDENT.ID
ORDER BY COUNT(TAKES.ID) DESC;
By using LEFT JOIN
, you can capture a student whose ID does not appear in the TAKES table. 通过使用LEFT JOIN
,您可以捕获ID在TAKES表中未出现的学生。
只需将结果分组即可。
SELECT COUNT(*), s.id FROM student s, takes t where t.id = s.id group by s.id order by count(*) desc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.