mysql獲取排名

2021-10-03 22:52:26 字數 712 閱讀 6361

mysql計算排名,獲取行號rowno

學生成績表資料

select * from table_score order by score desc;
獲取某個學生成績排名並計算該學生和上一名學生成績差,是並列排名

select *,

(select count(distinct score) from table_score as b where a.scorea.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獲取所有學生成績排名,不是並列排名。計算行號進行排名

select a.*,

(@rownum:=@rownum+1) as rank #計算行號

from table_score as a,

(select (@rownum :=0) ) b

order by a.score desc;

mysql分組排名 mysql分組排名

1.建立表並寫入測試資料 create table tb rank score city varchar 20 score int insert into tb rank score values sz 89 insert into tb rank score values sz 76 insert...

mysql 排名 MySQL用變數實現排名

mysql 8.0版本用視窗函式就可以實現排名,有三種方式,對相同值的處理不同 以上區別會在文末舉例,本文主要討論用變數實現排名 5.5版本用不了視窗函式 至少排序視窗用不了,其他的沒試過 那麼對於要顯示排名的需求就得想其他辦法啦,看網上的資料可以用變數來實現,來看看 首先建表並插入資料 資料資料來...

mysql儲存函式查詢排名 MySQL排名函式

應用場景 編寫乙個 sql 查詢來實現分數排名。如果兩個分數相同,則兩個分數排名 rank 相同。請注意,平分後的下乙個名次應該是下乙個連續的整數值。換句話說,名次之間不應該有 間隔 id score 1 3.50 2 3.65 3 4.00 4 3.85 5 4.00 6 3.65 例如,根據上述...