使用MySQL,如何选择某一特定行的查询结果等级? [英] Using MySQL, how do I select query result rank of one particular row?

查看:70
本文介绍了使用MySQL,如何选择某一特定行的查询结果等级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天已经花了很多时间尝试各种事情,但是似乎没有一个起作用.这是我的情况,我希望能够根据特定排序行的ID来选择行的排名

I've spent quite a lot of time today trying various things, but none of them seem to work. Here's my situation, I'd like to be able to select the rank of a row based on it's ID from a specifically sorted row

例如,如果我的查询类似:

For example, if my query is something like:

SELECT id, name FROM people ORDER BY name ASC

结果如下:


id   name
3    Andrew
1    Bob
5    Joe
4    John
2    Steve

我想获得排名(结果最后一行),而不返回所有行并循环抛出它们,直到获得想要的行(在PHP中).

I would like to get the rank (what row it ends up in the results) without returning all the rows and looping throw them until I get to the one I want (in PHP).

例如,我想选择史蒂夫"的等级",以便在这种情况下返回5(不是他的ID,而是上面查询中他名字的等级")

For example, I'd like to select the 'rank' of 'Steve' so that it returns -- in this case -- 5 (not his id, but the 'rank' of his name in the above query).

同样,我希望能够选择ID为1的行的排名.对于此示例,我想返回的"rank"为2(因为在结果行中ID为1),并且没有其他内容.

Similarly, I'd like to be able to select the rank of whatever row has the ID of 1. For this example, I'd like to return a 'rank' of 2 (because that's in what result row there is an ID of 1) and nothing else.

我已经尽可能多地获得了Google的不同结果……要么对大型表的查询速度非常慢,要么不得不创建各种临时表和用户变量(我非常想避免使用前者) ,我想我可以忍受).

I've Google'd as much as I could with varying results... either having really slow queries for larger tables or having to create all sorts of temporary tables and user variables (the former I'd REALLY like to avoid, the latter I suppose I can live with).

任何帮助或见识将不胜感激.

Any help or insight would be greatly appreciated.

推荐答案

来自 查看全文

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