分數排名 mysql mysql 分數排名

2021-10-17 13:17:39 字數 1011 閱讀 7690

編寫乙個 sql 查詢來實現分數排名。如果兩個分數相同,則兩個分數排名(rank)相同。請注意,平分後的下乙個名次應該是下乙個連續的整數值。換句話說,名次之間不應該有「間隔」。

| id | score |

| 1 | 3.50 |

| 2 | 3.65 |

| 3 | 4.00 |

| 4 | 3.85 |

| 5 | 4.00 |

| 6 | 3.65 |

例如,根據上述給定的 scores 表,你的查詢應該返回(按分數從高到低排列):

| score | rank |

| 4.00 | 1 |

| 4.00 | 1 |

| 3.85 | 2 |

| 3.65 | 3 |

| 3.65 | 3 |

| 3.50 | 4 |

create table if not exists scores (id int, score decimal(3,2))

truncate table scores

insert into scores (id, score) values ('1', '3.5')

insert into scores (id, score) values ('2', '3.65')

insert into scores (id, score) values ('3', '4.0')

insert into scores (id, score) values ('4', '3.85')

insert into scores (id, score) values ('5', '4.0')

insert into scores (id, score) values ('6', '3.65')

select score ,(select count(distinct score) from scores where score>=s.score)as rank from scores as s order by s.score desc;

分數排名 mysql MYSQL分數排名

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

分數排名 mysql mysql的分數排名處理

問題 將資料庫score表排名 返回排名前10的 db有兩種方法 1 效率不高,因為有子查詢。但是簡潔。而且我對socres表做了index。所以效能上也差 不了多少。mysql show create table scores g 1.row table scores create table c...

178 分數排名

編寫乙個 sql 查詢來實現分數排名。如果兩個分數相同,則兩個分數排名 rank 相同。請注意,平分後的下乙個名次應該是下乙個連續的整數值。換句話說,名次之間不應該有 間隔 例如,根據上述給定的 scores 表,你的查詢應該返回 按分數從高到低排列 例如,根據上述給定的 scores 表,你的查詢...