排名次(两个表交错连接)

示例

  输入: 
  Scores 表:
  +----+-------+
  | id | score |
  +----+-------+
  | 1  | 3.50  |
  | 2  | 3.65  |
  | 3  | 4.00  |
  | 4  | 3.85  |
  | 5  | 4.00  |
  | 6  | 3.65  |
  +----+-------+
  输出: 
  +-------+------+
  | score | rank |
  +-------+------+
  | 4.00  | 1    |
  | 4.00  | 1    |
  | 3.85  | 2    |
  | 3.65  | 3    |
  | 3.65  | 3    |
  | 3.50  | 4    |
  +-------+------+

解决

  select score, 
  (
  	select count(distinct b.score)  
  	from scores b
  	where b.score >= a.score
  ) as 'rank'
  from scores a
  order by score desc
posted @ 2022-03-01 11:12  jsqup  阅读(23)  评论(0编辑  收藏  举报