2012-01-07 69 views
1

我有以下兩個表(學生和標記):在student表我有關於學生的信息和在marks表我有關於學生已經得到的標記的信息。現在可以請你告訴我如何找出誰得到最高分,從marks表中計算得到的總分數?如何找出最高的總價值

在此先感謝:)

p.s.有些人可能會發現這個問題重複 - 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 
+0

哇...查理·辛是非常聰明的獲得學生信息。 – cmbuckley 2012-01-07 16:14:40

+0

@cbuckley是的,他是.. :)他是我最喜歡的演員之一。 :) – 2012-01-07 17:23:20

回答

3

這將獲得標記的最高和返回的人:

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 

在你的餐桌展望儘管如此,也許它是更有用的返回最高平均測試sco重新獲得返回他們的平均百分比。你可以用下面的方法做到這一點我相信:

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; 
+0

非常感謝GordyD。它正在按我想要的方式工作。 :) 謝謝:) – 2012-01-07 17:22:07

0

如果你從標記表中選擇了一個結果,它通過獲取標記字段(降序)排序。然後你會獲得最高分。

SELECT * FROM `marks` ORDER BY `obtainedmark` LIMIT 0, 1 

從這裏你可以然後通過使用ID從標識表

1
select s.studentId,s.student_name,sum(m.obtainedmark) as TotalMarks 
from Student s 
inner join Marks m 
on m.studentid=s.studentid 
group by s.studentId,s.student_name 
order by TotalMarks desc 
LIMIT 1