在mysql查询中获得一行的排名教程
我正在使用此查询根据他们获得的投票为每个名称分配排名,但它返回错误:
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.任何帮助,如何获得第一名的排名?
注意:我不是在寻找任何替代解决方案.只是尝试在查询本身中执行此操作.
解决方法:
错误很清楚.每个派生表都必须有自己的别名.你需要别名(SELECT @rownum:= 0),如下所示:
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