Skip to content

Commit 68c3120

Browse files
committed
feat: add mysql5 solutions to leetcode problems: No.0178
1 parent 7bad9c9 commit 68c3120

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

solution/0100-0199/0178.Rank Scores/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
<!-- tabs:start -->
4747

48-
### **SQL**
48+
### **MySQL8**
4949

5050
使用 `DENSE_RANK()` 函数,语法如下:
5151

@@ -71,4 +71,19 @@ SELECT Score, DENSE_RANK() OVER (ORDER BY Score DESC) 'Rank'
7171
FROM Scores;
7272
```
7373

74+
### **MySQL5**
75+
76+
MySQL 8 开始才提供了 `ROW_NUMBER()``RANK()``DENSE_RANK()`[窗口函数](https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html),在之前的版本,可以使用变量实现类似的功能:
77+
78+
```sql
79+
SELECT Score,
80+
CONVERT(rk, SIGNED) `Rank`
81+
FROM (SELECT Score,
82+
IF(@latest = Score, @rank, @rank := @rank + 1) rk,
83+
@latest := Score
84+
FROM Scores,
85+
(SELECT @rank := 0, @latest := NULL) tmp
86+
ORDER BY Score DESC) s;
87+
```
88+
7489
<!-- tabs:end -->

solution/0100-0199/0178.Rank Scores/README_EN.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
<!-- tabs:start -->
4343

44-
### **SQL**
44+
### **MySQL8**
4545

4646
Use `DENSE_RANK()` to solve this problem.
4747

@@ -60,4 +60,19 @@ SELECT Score, DENSE_RANK() OVER (ORDER BY Score DESC) 'Rank'
6060
FROM Scores;
6161
```
6262

63+
### **MySQL5**
64+
65+
MySQL only provides [window function](https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html) after version 8. In previous versions, variables can be used to achieve similar functions:
66+
67+
```sql
68+
SELECT Score,
69+
CONVERT(rk, SIGNED) `Rank`
70+
FROM (SELECT Score,
71+
IF(@latest = Score, @rank, @rank := @rank + 1) rk,
72+
@latest := Score
73+
FROM Scores,
74+
(SELECT @rank := 0, @latest := NULL) tmp
75+
ORDER BY Score DESC) s;
76+
```
77+
6378
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SELECT Score,
2+
CONVERT(rk, SIGNED) `Rank`
3+
FROM (SELECT Score,
4+
IF(@latest = Score, @rank, @rank := @rank + 1) rk,
5+
@latest := Score
6+
FROM Scores,
7+
(SELECT @rank := 0, @latest := NULL) tmp
8+
ORDER BY Score DESC) s;

0 commit comments

Comments
 (0)