使用MySQL,如何选择某一特定行的查询结果排名?

发布于 2024-08-01 18:16:46 字数 567 浏览 6 评论 0原文

今天我花了很多时间尝试各种事情,但似乎都不起作用。 这是我的情况,我希望能够根据特定排序行中的 ID 选择行的排名

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

SELECT id, name FROM people ORDER BY name ASC

结果如下:

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

我想获得排名(什么row 它最终出现在结果中),而不返回所有行并循环抛出它们,直到我到达我想要的行(在 PHP 中)。

例如,我想选择“Steve”的“排名”,以便在本例中返回 5(不是他的 id,而是上面查询中他姓名的“排名”)。

同样,我希望能够选择 ID 为 1 的任何行的排名。对于此示例,我希望返回 2 的“排名”(因为在结果行中,ID 为1)没有别的。

我已经尽可能多地用谷歌搜索不同的结果...要么对较大的表的查询非常慢,要么必须创建各种临时表和用户变量(前者我真的很想避免,后者我想我可以忍受)。

任何帮助或见解将不胜感激。

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

with results like:

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

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).

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).

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷了相思 2024-08-08 18:16:46

我尝试这段代码...可能会帮助其他人
我从用户那里获取结果,并上传用户个人资料图片表并加入它,而不是在我计算用户积分并对它们进行排序之后。
最后检查并添加行号以对用户进行排名...
享受它。
谢谢

set @row_num = 0;
set @calp =0;
select  if(@calp=(@calp:=user.cal_points), @row_num, @row_num := @row_num + 1) as row_number,user.* from 
(select user_skills.*,users.username,upload.file_name from user_skills join users on user_skills.user_id=users.id join upload on upload.upload_id=users.profile_pic order by user_skills.cal_points desc) as user
WHERE user.skill_name LIKE  '%ph%'

I try to this code... may help other
I am getting result from users and upload table for user profile pic and join it than after i am calculating user points and sorting on them.
At last am checking and adding row number for ranking for users...
Injoy it.
thanks

set @row_num = 0;
set @calp =0;
select  if(@calp=(@calp:=user.cal_points), @row_num, @row_num := @row_num + 1) as row_number,user.* from 
(select user_skills.*,users.username,upload.file_name from user_skills join users on user_skills.user_id=users.id join upload on upload.upload_id=users.profile_pic order by user_skills.cal_points desc) as user
WHERE user.skill_name LIKE  '%ph%'
烟沫凡尘 2024-08-08 18:16:46

来自artfulsoftware

SELECT p1.id, p1.name, COUNT( p2.name ) AS Rank
    FROM people p1
    JOIN people p2 
    ON p1.name < p2.name
    OR (
         p1.name = p2.name
         AND p1.id = p2.id
    )
GROUP BY p1.id, p1.name
ORDER BY p1.name DESC , p1.id DESC
LIMIT 4,1

from artfulsoftware:

SELECT p1.id, p1.name, COUNT( p2.name ) AS Rank
    FROM people p1
    JOIN people p2 
    ON p1.name < p2.name
    OR (
         p1.name = p2.name
         AND p1.id = p2.id
    )
GROUP BY p1.id, p1.name
ORDER BY p1.name DESC , p1.id DESC
LIMIT 4,1
氛圍 2024-08-08 18:16:46

像这样的东西吗?

SELECT Row, id, name
FROM (SELECT @row := @row + 1 AS Row, id, name
      FROM people
      ORDER BY name ASC)
WHERE Row = @SomeRowNumber

如果您想通过 ID,只需更改 where 子句即可。

Something like this?

SELECT Row, id, name
FROM (SELECT @row := @row + 1 AS Row, id, name
      FROM people
      ORDER BY name ASC)
WHERE Row = @SomeRowNumber

If you want to go by the ID, just alter the where clause.

沫离伤花 2024-08-08 18:16:46

尝试这个:

SELECT @rownum:=@rownum+1 `rank`, p.id, p.name
FROM people p, (SELECT @rownum:=0) r
ORDER BY name ASC

Try this:

SELECT @rownum:=@rownum+1 `rank`, p.id, p.name
FROM people p, (SELECT @rownum:=0) r
ORDER BY name ASC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文