MySQL更新语句存储排名位置
我正在尝试解决一个问题,但我就是想不通。如果有人给我指点,我将不胜感激。作为我想要实现的目标的一个简单示例,我在数据库中有这些记录,
Score|Ranking
-------------
100 |0
200 |0
300 |0
并且我希望排名字段根据得分最高的人包含 1,2,3,因此结果应该是:
Score|Ranking
-------------
100 |3
200 |2
300 |1
目前,我正在为所有这些记录执行 for next 循环,但考虑到实际上可能有几千个 - 这可能需要永远!有谁知道可以一次性完成此操作的神奇查询吗?
I'm trying to get my head around a query and I just can't figure it out. I would appreciate if someone give me a pointer. As a simple example of what I'm trying to achieve, I have these records in the database
Score|Ranking
-------------
100 |0
200 |0
300 |0
And I would like the Ranking field to contain 1,2,3 based on who's got the highest score so the result should be:
Score|Ranking
-------------
100 |3
200 |2
300 |1
At the moment, I'm doing a for next loop for all these records but given that in reality that could be a few thousand - that could take forever! Does anyone have an idea on a magic query which would do this in one go?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一种方法:
Here's a way to do it:
在 MySQL 中,您可以使用 row_number。
这是一个使用它的示例在
SELECT
中:如果您使用这样的
SELECT
INSERT INTO
,您将获得排名。In MySQL, you can use row_number.
Here's an example of using it in a
SELECT
:If you
INSERT INTO
using aSELECT
like this, you will get your rankings.这将创建一个内联更新语句,该语句将按变量
@rc
递增对玩家进行排名。我在非常类似的情况下使用过它很多次,它运行良好并将其全部保留在数据库端。假定
id
是您的players
表的主键。This creates an inline update statement that will rank your players incrementing by the variable
@rc
. I've used it many times in very similar cases, it works well and keeps it all on the DB side.id
is assumed to be the primary key for yourplayers
table.如果您使用的是 MySQL 8,那么您可以使用新函数 RANK()
根据您想要如何显示平均分数的排名,您也可以查看 DENSE_RANK()
作为更新:
If you are using MySQL 8 so can you use the new function RANK()
Depending on how you want to display the ranking with even score so can you also check out DENSE_RANK()
And as an UPDATE:
我正在向您展示我的做法[对于间隔sql更新功能]
select:
update:
除了测试它是否有效之外,我没有对此进行任何性能检查
i'm showing you my way of doing it [for interval sql update functions]
select:
update:
i did not make any performance checks on this one except for testing that it works