gpt4 book ai didi

mysql - 使用mysql获取当前排名

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

我有这个查询来获取行的排名,按某些字段排序

SET @rank=0;
SELECT @rank:=@rank+1 AS rank, id, ammopacks
FROM users
GROUP BY id
ORDER BY ammopacks DESC;

如何获得“x”id 的排名,而不是之前对它们进行排名?我不想知道所有的用户排名,只想知道一个 id。

这可能吗?

提前致谢

最佳答案

您可以使用子查询来做到这一点:

select count(*) as rank
from users u
where u.ammopacks >= (select ammopacks from users u2 where u2.id = x)

这并不完全相同。这将进行真正的排名,具有相同 ammopacks 值的用户将具有相同的排名。在这种情况下,原始版本会为不同的用户提供不同的顺序值。

要获得这种效果,您可以这样做:

select count(*) as rank
from users u
where u.ammopacks > (select ammopacks from users u2 where u2.id = x) or
(u.ammopacks = (select ammopacks from users u2 where u2.id = x) and
u.id <= x
)

关于mysql - 使用mysql获取当前排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16451670/

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