4

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 - Every derived table must have its own alias

Here is my code:

SELECT @rownum:=@rownum+1 AS rank, name, vote 
FROM table, (SELECT @rownum:=0) ORDER BY vote DESC

On modifying the query to this :-

SELECT @rownum:=@rownum+1 AS rank, name, vote 
FROM table ORDER BY vote DESC

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.

4

1 回答 1

6

错误很明显。每个派生表都必须有自己的别名。你需要这样别名(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

SQL 小提琴演示

于 2012-11-03T12:45:04.993 回答