[LeetCode] Rank Scores -- 数据库知识(mysql)

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    |
+-------+------+

解法:

1. With Variables(用户定义变量): 700 ms

First one uses two variables, one for the current rank and one for the previous score.

SELECT
  Score,
  @rank := @rank + (@prev <> (@prev := Score))  Rank
FROM
  Scores,
  (SELECT @rank := 0, @prev := -1) init
ORDER BY Score desc

这种方法主要的思想就是基于变量。关键点是   选择排名和上一个分数 两个变量,  变量的初始化,判断相同的分数的排名。mysql不像SQL有4个排名的函数可以调用,因此要自己来写排名的功能。

类似的实现:

SELECT Score, Rank FROM(
  SELECT    Score,
            @curRank := @curRank + IF(@prevScore = Score, 0, 1) AS Rank, @prevScore := Score
  FROM      Scores s, (SELECT @curRank := 0) r, (SELECT @prevScore := NULL) p
  ORDER BY  Score DESC
) t;

2. Always Count: 1322 ms

This one counts, for each score, the number of distinct greater or equal scores.

SELECT
  Score,
  (SELECT count(distinct Score) FROM Scores WHERE Score >= s.Score) Rank
FROM Scores s
ORDER BY Score desc

没太看懂这种算法的思想。

补充知识:Mysql 用户自定义变量详解

你可以利用SQL语句将值存储在用户自定义变量中,然后再利用另一条SQL语句来查询用户自定义变量。这样以来,可以再不同的SQL间传递值。

用户自定义变量的声明方法形如:@var_name,其中变量名称由字母、数字、“.”、“_”和“$”组成。当然,在以字符串或者标识符引用时也可以包含其他字符(例如:@’my-var’,@”my-var”,或者@`my-var`)。

用户自定义变量是会话级别的变量。其变量的作用域仅限于声明其的客户端链接。当这个客户端断开时,其所有的会话变量将会被释放。用户自定义变量是不区分大小写的。

使用SET语句来声明用户自定义变量:

1 SET @var_name = expr[, @var_name = expr] ...

在使用SET设置变量时,可以使用“=”或者“:=”操作符进行赋值。当然,除了SET语句还有其他赋值的方式。比如下面这个例子,但是赋值操作符只能使用“:=”。因为“=”操作符将会被认为是比较操作符。

mysql> SET @t1=1, @t2=2, @t3:=4;
mysql> SELECT @t1, @t2, @t3, @t4 := @[email protected][email protected];
时间: 2024-10-19 05:08:35

[LeetCode] Rank Scores -- 数据库知识(mysql)的相关文章

[LeetCode] Rank Scores 分数排行

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" be

[LeetCode]Rank Scores,解题报告

题目 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"

178.LeetCode Rank Scores

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" be

[LeetCode] Department Highest Salary -- 数据库知识(mysql)

184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--

LeetCode - Rank Scores

对分数排序  参考了网上的答案  而且如果不Order by 有一个BUG就是 如果表为空则什么都不输出包括null才是正确的. # Write your MySQL query statement below select s.Score, count(rank.Score) as Rank from Scores s,( select distinct Score from Scores ) rank where s.Score <= rank.Score group by s.Id ord

LeetCode:Rank Scores - 按分数排名次

1.题目名称 Rank Scores(按分数排名次) 2.题目地址 https://leetcode.com/problems/rank-scores/ 3.题目内容 按分数排名次,如果两个Id的分数一样,那么他们的名次是一样的,排名从1开始.注意,每组分数的名次,都是上一组分数名次加一. 例如,有这样一组数据: +----+-------+ | Id | Score | +----+-------+ | 1  | 3.50  | | 2  | 3.65  | | 3  | 4.00  | | 

[LeetCode][SQL]Rank Scores

Rank Scores 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 "ho

LeetCode Database: Rank Scores

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" be

Mysql数据库知识总结(看资料总结出来的)

毕业到现在算起来做了3年多服务端开发了,毕业之后很少有时间想在学校一样可以抽出一些空余的时间对知识进行一个总结,到现在也是时候对一些关键的知识一个总结,今天趁着时间比较多,先来对用了3年多的开源关系型数据库mysql进行一下总结,整理了一下知识点可以分为以下几点进行: 一.基础知识 二.SQL优化与索引 三.数据库规范建议 四.数据库设计 五.数据库架构 一.基础知识 知识点主要包括:数据类型 常用函数 字符集 事务隔离级别 锁机制 (1).数据类型 数值类型 -- TINYINT.SMALLI