在MYSQL中生成排名的最佳方法是什么? [英] What is the best way to generate ranks in MYSQL?
本文介绍了在MYSQL中生成排名的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
除MYSQL中的行数据外,获取行的排名的最佳方法是什么?
What's the best way to get the rank of the rows in addition to the row data in MYSQL?
例如,说我有一个学生名单,我想在GPA上排名.我知道我可以按GPA排序,但是让MYSQL在返回的行数据中也能返回排名的最快方法是什么?
For instance, say I have a list of students and I want to rank on the GPA. I know I can order by the GPA, but what's the quickest way to have MYSQL return the rank as well in the rowdata I get back?
推荐答案
这将返回排名为rownum
SELECT @rownum := @rownum + 1 rownum,
t.*
FROM (SELECT @rownum:=0) r,
(SELECT * FROM students ORDER BY gpa DESC) t;
这篇关于在MYSQL中生成排名的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文