在MySQL查询中获取行的排名 [英] Getting rank of a row in mysql query
本文介绍了在MySQL查询中获取行的排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此查询根据他们获得的投票为每个姓名分配等级,但是返回错误:
I was using this query to assign rank to every name according to the votes they have got, but it returns with the error :
1248-每个派生表必须具有自己的别名
1248 - Every derived table must have its own alias
这是我的代码:
SELECT @rownum:=@rownum+1 AS rank, name, vote
FROM table, (SELECT @rownum:=0) ORDER BY vote DESC
在将查询修改为此:-
SELECT @rownum:=@rownum+1 AS rank, name, vote
FROM table ORDER BY vote DESC
我得到的查询排名为NULL.有什么帮助,如何获得第一名?
I get as expected rank of the queries as NULL. Any help , how to get rank at first place ?
注意:我不需要任何其他解决方案.只是尝试在查询本身中做到这一点.
NOTE: I am not looking for any alternative solution. Just trying to do it in the query itself.
推荐答案
错误非常明显.每个派生表必须具有自己的别名.您需要像这样为(SELECT @rownum := 0)
加上别名:
The error is pretty clear. Every derived table must have its own alias. You need to alias the (SELECT @rownum := 0)
like so:
SELECT
@rownum := @rownum + 1 AS rank,
name,
vote
FROM table, (SELECT @rownum := 0) t --This what you were missing an alias
ORDER BY vote DESC
这篇关于在MySQL查询中获取行的排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文