gpt4 book ai didi

mysql - mysql查询从总分中获取排名时出错

转载 作者:行者123 更新时间:2023-11-29 06:49:04 26 4
gpt4 key购买 nike

我想根据学生表中的总分获得排名。

SET @PreviousRecord = NULL;
SET @Rank = 0;
SELECT studentid,total,result CASE
WHEN @PreviousRecord = total
THEN @Rank
WHEN @PreviousRecord := total
THEN @Rank := @Rank + 1
END AS ranks
FROM studentdetails

ORDER BY total;

它不工作,出现错误

20 errors were found during analysis. Unrecognized keyword. (near "CASE" at position 30) Unrecognized keyword. (near "WHEN" at position 37) Unexpected token. (near "@PreviousRecord" at position 42) Unexpected token. (near "=" at position 58) Unexpected token. (near "total" at position 60) Unrecognized keyword. (near "THEN" at position 80) Unexpected token. (near "@Rank" at position 85) Unrecognized keyword. (near "WHEN" at position 93) Unexpected token. (near "@PreviousRecord" at position 98) Unexpected token. (near ":=" at position 114) Unexpected token. (near "total" at position 117) Unrecognized keyword. (near "THEN" at position 137) Unexpected token. (near "@Rank" at position 142) Unexpected token. (near ":=" at position 148) Unexpected token. (near "@Rank" at position 151) Unexpected token. (near "+" at po

我的表结构是

enter image description here

最佳答案

SET @Rank = 0;
SELECT @Rank := @Rank+1,Z.studentid,Z.total
FROM
(
SELECT studentid,total
FROM studentdetails order by total
)Z

尝试上面的查询。

关于mysql - mysql查询从总分中获取排名时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48208132/

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