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. 初学ubuntu命令

    将我第一天学的内容做个笔记 ,方便日后查看 命令的使用: ls     ls -a 显示所有文件  ls -l 显示文件的所有信息 cp   注意命令格式  /home/../temp/a   /ho ...

  2. SqlServer执行Insert命令同时判断目标表中是否存在目标数据

    针对于已查询出数据结果, 且在程序中执行Sql命令, 而非数据库中的存储过程 INSERT INTO TableName (Column1, Column2, Column3, Column4, Co ...

  3. django重定向

    return HttpResponseRedirect('/index/')# 重定向返回url格式:http://127.0.0.1:8000/index/会去掉前期的所有路由重新写入/index/ ...

  4. 使用netstat命名排查网络问题的参考指引

    原文链接:http://www.lookdaima.com/WebForms/WebPages/Blanks/Pm/Docs/DocItemDetail.aspx?id=69b487d0-8bf4-4 ...

  5. C# -- 文件的压缩与解压(GZipStream)

    文件的压缩与解压 需引入 System.IO.Compression; 1.C#代码(入门案例) Console.WriteLine("压缩文件..............."); ...

  6. 【算法】LeetCode算法题-Palindrome Number

    这是悦乐书的第144次更新,第146篇原创 今天这道题和回文有关,即从前往后和从后往前是一样的,如"上海自来水来自海上"就是一个回文字符串,如整数121就是回文数,这些都是和回文相 ...

  7. ab和jmeter进行GET/POST压力测试的使用心得和比较

    引言: 互联网服务压测是非常重要的评价方法,ab,webbench,jmeter等都是业界流行的测试工具,ab和webbench作为shell模式下轻量级的测试工具,jmeter则作为有GUI界面的更 ...

  8. 16.ajax_case02

    # 抓取当当网书评 # http://product.dangdang.com/25340451.html import json import requests from lxml import e ...

  9. 发现电脑上装着liteide,就用golang做一个TCP通讯测试(支持先启动client端和断线重连)

    1.参考https://www.cnblogs.com/yin5th/p/9274495.html server端 main.go package main import ( "fmt&qu ...

  10. Python:Day09

    Ubantu忘记密码: 1.开机长按shift,进入界面后按e: 2.将红框中内改成如下并按F10重启: 3.输入passwd,然后用户名,然后重新输入密码: locale命令查看系统中是否有中文 a ...