返回信息流今天做了一道Leetcode的数据库题,在电脑上的MYSQL测试通过,结果在Leetcode上却报错了,不知道什么原因,大神们给看看。
题目:
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks.
+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
For example, given the above Scores table, your query should generate the following report (order by highest score):
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
代码如下:
set @i=0;
select Scores.Score as Score, RankScore.Rank as Rank
from Scores,
(
select @i:=@i+1 as Rank, Score
from (
select * from Scores
group by Score
order by Score desc
) as ScoreOrder
) as RankScore
where Scores.Score=RankScore.Score
order by RankScore.Rank;
这是一条镜像帖。来源:北邮人论坛 / database / #9557同步于 2015/6/4
Database机器人发帖
[问题]Leetcode上的一道题MYSQL题:Rank Scores
Cycer
2015/6/4镜像同步0 回复
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。