简体   繁体   English

我如何在mysql工作台中找到此查询

[英]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.

相关问题 MYSQL 工作台。 如何计算 sql (mysql workbench) 中的年龄? 然后,在数据库中找到年龄的比例 - MYSQL Workbench. How can I calculate age in sql (mysql workbench)? Then, find the ratio of age in database 如何设置MySQL Workbench的样式 - how can i style mySQL Workbench 我如何在 mysql 工作台中查找、检查和更改我的 $db_host、用户、密码、名称? - How can I find out, check and change my $db_host, user, pass, name in mysql workbench? 如何使用MySQL Workbench在字符串中找到子字符串的多次出现? - How can I find multiple occurrences of a substring within a string using MySQL Workbench? MySQL Workbench如何使用文件的每一行作为输入来运行相同的查询? - MySQL Workbench How can I run the same query using each line of the file as input? 如何在Perl中提高MySQL查询的性能,在MySQL Workbench中直接执行的查询速度要快1600倍 - How can I improve the performance of this MySQL Query in Perl, the same Query directly executed in MySQL Workbench is 1600 times faster 如何在Mysql WorkBench中进行查询? - How to do a query in Mysql WorkBench? 如何通过 mysql workbench 将数据导入 mysql 数据库? - How can I import data into mysql database via mysql workbench? MySQL Workbench:当我使用“应用”按钮时,如何查看详细的SQL查询和错误? (确认弹出窗口) - MySQL Workbench: How can I see the detailed SQL query and errors when I use the “Apply” button? (confirmation popup) 如何计算在mysql工作台中一天执行的事务? - How can i count transactions executed in a day in mysql workbench?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM