在 MySQL 表上创建数字顺序索引 [英] Creating a numerical order index on a MySQL table

查看:53
本文介绍了在 MySQL 表上创建数字顺序索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(基本上)有 3 列的表格 - 姓名、投票和排名.我想按票数对表格进行排序,然后更新排名"以反映此顺序,以便获得票数最多的将排名设置为 1,第二多的票为 2,以此类推.

I have a table with (essentially) 3 columns - name, votes and rank. I want to order the table by votes and then update 'rank' to reflect this order, so that the one with the most votes will have rank set to 1, the second most votes to 2 etc.

我可以在 PHP 中执行此操作,但似乎很浪费 - 有没有一种方法可以通过一个 SQL 查询来执行此操作,而无需手动处理 PHP 中的每条记录?

I can do this in PHP but it seems pretty wasteful - is there a way to do this with one SQL query without having to manually process every record in PHP?

推荐答案

虽然@johnfx 给了你正确的答案我想你会感兴趣阅读这篇文章的评论 - http://arjen-lentz.livejournal.com/55083.html 解决了各种排名问题.

Although @johnfx gave you the correct answer I think you will be interested reading the comments of this article - http://arjen-lentz.livejournal.com/55083.html where various ranking problems are solved.

这篇关于在 MySQL 表上创建数字顺序索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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