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 := @t1+@t2+@t3;

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

  1. Mysql数据库知识-Mysql索引总结 mysql mysql数据库 mysql函数

    mysql数据库知识-Mysql索引总结: 索引(Index)是帮助MySQL高效获取数据的数据结构. 下边是自己整理的资料与自己的学习总结,,做一个汇总. 一.真的有必要使用索引吗? 不是每一个性能 ...

  2. [LeetCode] Rank Scores 分数排行

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  3. LeetCode——Rank Scores

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  4. [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)

    1. 题目名称   Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...

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

    184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...

  6. LeetCode:Rank Scores

    做到这题时卡了不少时间,参考了别人的解法,觉得挺不错的,还挺巧妙. SELECT s2.Score,s1.Rank From ( SELECT S1.Score, COUNT(*) as Rank F ...

  7. MySQL中变量的用法——LeetCode 178. Rank Scores

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  8. LeetCode Database: Rank Scores

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  9. MySQL篇,第一章:数据库知识1

    MySQL 数据库 1 一.MySQL概述 1.什么是数据库       数据库是一个存储数据的仓库 2.哪些公司在用数据库       金融机构.购物网站.游戏网站.论坛网站... ... 3.提供 ...

随机推荐

  1. 自动化测试基础篇--Selenium select下拉框

    摘自https://www.cnblogs.com/sanzangTst/p/7681523.html 一.什么是下拉框 下拉框是多项选择项,选择其中一种,类似下面(以百度搜索设置为例) 源代码如下所 ...

  2. jenkins 备份配置信息

    本文介绍几种备份jenkin配置信息的方法,大家可根据实际情况做出选择. 我的测试环境如下: windows 7 jenkins 2.32.3 ____升级到___2.46.3 (长期支持版本) 多种 ...

  3. Docker 入门到实践(四)Docker 使用镜像

    一.获取镜像 Docker Hub 上有大量的高质量的镜像让我们获取,命令为: docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签] 具体的选项可以 ...

  4. (转)Spring Boot(七):Mybatis 多数据源最简解决方案

    http://www.ityouknow.com/springboot/2016/11/25/spring-boot-multi-mybatis.html 说起多数据源,一般都来解决那些问题呢,主从模 ...

  5. MySQL JDBC驱动版本与MySQL数据库版本对应关系

    前言:前段时间发现在家使用和公司一样的mysql jdbc驱动版本发生了异常,原因:家里mysql数据库版本与公司不一致导致.查询了相关资料,发现mysql jdbc驱动版本与mysql数据库版本有一 ...

  6. Redis 的安装 使用 通知事件

    Redis 的安装 使用 介绍: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string.list.set.zset(sorted ...

  7. 设计模式のFlyweight(享元模式)----结构模式

    一.产生背景 享元模式:它使用共享物件,用来尽可能减少内存使用量以及分享资讯给尽可能多的相似物件:它适合用于只是因重复而导致使用无法令人接受的大量内存的大量物件.通常物件中的部分状态是可以分享.常见做 ...

  8. 真正的Maven经常使用命令

    长期用Eclipse的Maven插件的小伙伴可能接触Maven的经常使用命令比較少.每次用每次翻文档. 假设让你脱离Eclipse怎么办,面试的时候考到了怎么办-- 假设你不想尴尬,请小朋友花点时间运 ...

  9. maven 经常使用命令

    版权声明:本文为博主原创文章,未经博主同意不得转载. 安金龙 的博客. https://blog.csdn.net/smile0198/article/details/25567541 刚開始用.记录 ...

  10. 前端导出excel表

    前端导出excel表 方式一: 前端js实现 : https://www.cnblogs.com/zhangym118/p/6235801.html 方式二: java后端实现: https://bl ...