gpt4 book ai didi

mysql - 通过Mysql查询动态获取排名

转载 作者:行者123 更新时间:2023-11-29 00:35:48 27 4
gpt4 key购买 nike

我有一个像这样的结果表:

ID  STUDENT_ID  Branch_id   Class_id    Exam_id Subject_id  Numbers     Date1       653         5           1           1   8               60      2012-01-012       653         5           1           1   9               40      2012-01-013       653         5           1           1   10              80      2012-01-014       653         5           1           1   11              50      2012-01-015       653         5           1           1   12              65      2012-01-016       653         5           1           1   13              33      2012-01-017       653         5           1           1   15              86      2012-01-018       222         5           1           1   8               100     2012-01-019       222         5           1           1   9               80      2012-01-0110      222         5           1           1   10              92      2012-01-0111      222         5           1           1   11              50      2012-01-0112      222         5           1           1   12              65      2012-01-0113      222         5           1           1   13              33      2012-01-017       222         5           1           1   15              86      2012-01-01

My Desire Result like:

Student_ID  Math    English      Science    Total   Rank1       90  89      88  267  12       90  89      88  267  13       58  45      98  201      2

I want to get student rank by this method:

Reference Link

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) 列上分配排名。

请帮助。

最佳答案

试试这个:

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;

关于mysql - 通过Mysql查询动态获取排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14352534/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com