php - 如何找出最高的总值

标签 php mysql

我有以下两个表(学生和分数):在 student 表中,我有关于学生的信息,在 marks 表中,我有关于学生分数的信息得到。现在你能告诉我如何从 marks 表中计算出总得分,从而找出谁获得了最高分吗?

提前致谢:)

附注有些人可能会发现此问题重复 - how to find the highest value in mysql table但在我的辩护中,我的问题略有不同。 :)

学生表:

studentid   student_name
 1001        Jon Bon Jovi
 1002        Charlie Sheen
 1003        Jason Statham
 ... Goes on like this

标记表:

id   studentid totalmark obtainedmark  
1     1001       20          12
2     1002       20          20
3     1003       20          15
4     1001       50          40
5     1002       50          50
6     1003       50          45     
... Goes on like this

最佳答案

这将返回获得分数总和最高的人:

SELECT s.studentid, s.student_name, SUM(m.obtainedmark) as c
FROM students s INNER JOIN marks m ON s.studentid = m.studentid
GROUP BY s.studentid
ORDER BY c DESC
LIMIT 1

尽管查看您的表格,返回最高平均测试分数 返回他们获得的平均百分比可能更有用。我相信您可以使用以下方法来做到这一点:

SELECT s.userId, s.name, AVG( ( m.obtainedmark / m.totalmark ) ) as c
FROM bnt_users s INNER JOIN bnt_user_skill_rating m ON s.userId = m.user_id
GROUP BY s.userId
ORDER BY c DESC
LIMIT 2;

关于php - 如何找出最高的总值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8771017/

相关文章:

php - 使用多对多关系 Laravel/Eloquent/查询构建器时如何从另一个表获取所有书籍

MYSQL 查找重复值的计数

php - 如何将 Joomla mySQL 数据库移动到 Mariadb

mysql - 从表中选择最小值(也包括最大值和平均值)以及事件的时间戳

php - password_hash、Password_default 导致问题

php - Cakephp - 一张表的多个模型

php - 如何从mysql数据库中检索特定数据

python /MySQL : Query executes but nothing happens?

php - 插入的数据不会进入 phpMyAdmin 数据库

php - 错误 2002 (HY000) : Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)