178. 分数排名

https://leetcode-cn.com/problems/rank-scores/

178. 分数排名

select s1.Score, count(distinct s2.Score) as Rank from scores as s1, scores as s2 where s1.Score <= s2.Score group by s1.Id order by s1.Score desc;

具体思路就是把一个表变成两个表,然后进行比较

select s1.Score, count(s2.Score) as Rank from Scores as s1, Scores as s2 where s1.Score <= s2.Score group by s1.Id order by s1.Score desc;
select * from Scores as s1, Scores as s2;
#执行顺序:
#from > on > join > where > group by > having > select > distinct > order by > limit
#第一步,from对两个表计算笛卡尔积
#第二步,where判断小于等于条件,进行筛选
#第三步,group by进行分组去重,这样,s2.scores多行就会被压在一行中
#第四步,distinct计算不同的个数
#第五步,order by进行排序

具体说一下为什么要用group by ,如果不用group by的话,原先的数据集是一个大组,只会对一行进行处理。而group by之后,是分成多个小组,然后每一个小组分别取进行处理

原文地址:https://www.cnblogs.com/letlifestop/p/12486023.html