MYSQL 5.7 获取行号 [英] MYSQL 5.7 Getting the row number

查看:72
本文介绍了MYSQL 5.7 获取行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为playerrank"的数据库,其中包含积分列.我想在人们的个人资料页面上显示他们的排名如下:

I have a database called "playerrank" that has points column. I want to show on people's profile page their rank like this:

等级:3/1456

我尝试使用 ROW_NUMBER() 但似乎我的主机版本较低(我相信是 5.7).它给了我错误.

I tried using ROW_NUMBER() but it seems like my host has low version (5.7 i believe).its giving me errors.

除了按点数对数据库进行排序并以某种方式获取行号之外,还有其他方法可以根据点数获得玩家的排名吗?

Is there another way i can get the ranking of a player based on points other than ordering the db by points desc and getting the row number somehow?

推荐答案

在 MySQL 5.7 中模拟行号的一个选项使用会话变量:

One option to simulate row number in MySQL 5.7 uses session variables:

SET @row_number = 0;

SELECT 
    (@row_number:=@row_number + 1) AS rnk, points
FROM yourTable
ORDER BY points DESC;

请注意,从技术上讲,行号与排名不同,但我怀疑您确实需要此处的行号.在这种情况下,如果说三名玩家的分数相同,他们可能会分配到不同的排名编号.

Note that technically row number is not the same thing as rank, but I suspect that you do want row number here. In this case, if say three players were tied with the same number of points, they might have a different rank number assigned to them.

这篇关于MYSQL 5.7 获取行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆