mysql計算排名,獲取行號rownomysql
SELECT * FROM table_score ORDER BY score DESC;
SELECT *, (SELECT count(DISTINCT score) FROM table_score AS b WHERE a.score<b.score)+1 AS rank, #獲取排名,並列 (SELECT b.score FROM table_score AS b WHERE b.score>a.score ORDER BY b.score LIMIT 1)-a.score AS subtract #獲取和上一名學生成績的差 FROM table_score AS a WHERE a.s_id = 13; #獲取學生週三的成績排名和與上一名的成績差
SELECT *, (SELECT count(DISTINCT score) FROM table_score AS b WHERE a.score<b.score)+1 AS rank #獲取排名-並列 FROM table_score AS a ORDER BY rank; #獲取學生成績排名
SELECT a.*, (@rowNum:=@rowNum+1) AS rank #計算行號 FROM table_score AS a, (SELECT (@rowNum :=0) ) b ORDER BY a.score DESC;