我有一个结果表,如:
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 Math English Science Total Rank 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(数字)列之后对所有数字进行排序。
请帮助。
答案 0 :(得分: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;