我有一个名为“MyTable”的MySQL表,它基本上列出了用户名和点(两列,名称和点)。我想说的是“什么是joe1928的等级?”,这当然是基于他的观点。我怎么能在MySQL中执行此操作而无需下载所有数据并对其进行排序并自行确定排名?
得分最高的人将排名第一。
答案 0 :(得分:1)
尝试获得分数高于用户的人数:
select count(*) from MyTable where score > (select score from MyTable where user = 'Joe');
这将为顶级用户返回0。
答案 1 :(得分:0)
This page seems to describe and solve your problem.
该页面的注释:
SET @rownum := 0;
SELECT rank, correct FROM (
SELECT @rownum := @rownum + 1 AS rank, correct, uid
FROM quiz_user ORDER BY correct DESC
) as result WHERE uid=xxxxxxxx
答案 2 :(得分:0)
SELECT @r AS Rank
FROM MyTable u, (SELECT @r := 0)
WHERE (@r := @r + 1) * (u.Username = 'joe1928')
ORDER BY u.Score DESC
LIMIT 1
答案 3 :(得分:0)
select * from [TABLENAME] where [USERNAME] = blah order by [POINTS] desc limit 1;
答案 4 :(得分:0)
根据@Dave发布的链接,您的查询将如下所示:
select Rank,name from
(select @rownum:=@rownum+1 AS 'Rank', p.name
from calls p, (select @rownum:=0) r
order by p.points desc) as rankResults
where name = 'joe';
答案 5 :(得分:0)
这是来自另一个堆栈溢出页面,似乎解决了你的问题。
SELECT uo.*,
(
SELECT COUNT(*)
FROM users ui
WHERE (ui.points, ui.id) >= (uo.points, uo.id)
) AS rank
FROM users uo
WHERE id = @id