1

我有一个结果表,如:

ID STUDENT_ID Branch_id Class_id Exam_id Subject_id Numbers Date
1 653 5 1 1 8 60 2012-01-01
2 653 5 1 1 9 40 2012-01-01
3 653 5 1 1 10 80 2012-01-01
4 653 5 1 1 11 50 2012-01-01
5 653 5 1 1 12 65 2012-01-01
6 653 5 1 1 13 33 2012-01-01
7 653 5 1 1 15 86 2012-01-01
8 222 5 1 1 8 100 2012-01-01
9 222 5 1 1 9 80 2012-01-01
10 222 5 1 1 10 92 2012-01-01
11 222 5 1 1 11 50 2012-01-01
12 222 5 1 1 12 65 2012-01-01
13 222 5 1 1 13 33 2012-01-01
7 222 5 1 1 15 86 2012-01-01

我的愿望结果如下:

Student_ID 数学 英语 科学 总排名
1 90 89 88 267 1
2 90 89 88 267 1
3 58 45 98 201 2


我想通过这种方法获得学生排名:

参考链接

SET @rank = 0, @prev_val = NULL;

SELECT rank, correct FROM 
(
    SELECT 
        @rank := IF(@prev_val=correct,@rank,@rank+1) AS rank,
        @prev_val := correct AS correct, uid
    FROM quiz_user 
    ORDER BY correct DESC
)as result WHERE uid=xxxxxxxxxxxx

这个查询我只需要表结构之间的区别,帖子的作者在正确的列上分配排名,我需要在对所有数字求和后在数字 SUM(numbers) 列上分配排名。

请帮忙。

4

1 回答 1

1

试试这个:

SELECT STUDENT_ID, Numbers, IF(@marks=(@marks:=Numbers), @auto, @auto:=@auto+1) rank 
FROM (SELECT STUDENT_ID, SUM(Numbers) Numbers
      FROM quiz_user 
      GROUP BY STUDENT_ID 
      ORDER BY Numbers DESC, STUDENT_ID 
     ) AS A, (SELECT @auto:=0, @marks:=0) AS B;
于 2013-01-16T06:37:14.670 回答