问题
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.
回答1:
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
SQL Fiddle Demo
来源:https://stackoverflow.com/questions/13209227/getting-rank-of-a-row-in-mysql-query