gpt4 book ai didi

php - mysql按分数/点数对用户进行排名

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:54 24 4
gpt4 key购买 nike

下面的查询对于按分数排名的普通用户来说很酷,但我需要一个具有相同排名结果的查询,以获得表中相同的分数

SET @rank = 0;
SELECT * FROM (
SELECT @rank:=@rank + 1 AS rank,score, user_id FROM site_crossword_members ORDER BY score DESC
) as tmp;

谢谢

最佳答案

您确定您给定的查询是否正常工作?我记得要在 SELECT 之后完成 ORDER BY,从而使您的排名按 ID ASC 或类似方式排序。

SELECT user_id, 
CASE WHEN @score != score THEN @rank := @rank + 1 ELSE @rank END AS rank,
@score := score AS dummy_value
FROM ( SELECT score, user_id
FROM site_crossword_members,
(SELECT @rank := 0, @score := NULL) AS vars
ORDER BY score DESC) AS h

简化版:

SELECT user_id, 
@rank := @rank + (@score != score) AS rank,
@score := score AS dummy_value
FROM ( SELECT score, user_id
FROM site_crossword_members,
(SELECT @rank := 0, @score := NULL) AS vars
ORDER BY score DESC) AS h

关于php - mysql按分数/点数对用户进行排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13393422/

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