惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
宝玉的分享
宝玉的分享
I
InfoQ
P
Privacy International News Feed
V
V2EX
IT之家
IT之家
S
SegmentFault 最新的问题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V2EX - 技术
V2EX - 技术
C
CERT Recently Published Vulnerability Notes
C
Check Point Blog
The Register - Security
The Register - Security
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
AWS News Blog
AWS News Blog
M
MIT News - Artificial intelligence
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
B
Blog
N
Netflix TechBlog - Medium
B
Blog RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
T
Threatpost
Forbes - Security
Forbes - Security
U
Unit 42
A
Arctic Wolf
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Palo Alto Networks Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recorded Future
Recorded Future
L
Lohrmann on Cybersecurity
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
月光博客
月光博客
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
I
Intezer
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
The Hacker News
The Hacker News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
MyScale Blog
MyScale Blog
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位

博客园 - 每天进步多一点

C#学习相关系列之Linq用法---group和join相关用法 linq group by having 实现 常用知识-T-SQL优化 mysql窗口函数、Mysql分析函数 MySQL系列三(定位慢SQL、索引优化、SQL优化)Using filesort MySQL 内存相关参数设置 MySQL COALESCE 函数使用详解 SQL性能优化指南:如何优化MySQL多表join场景 MySQL内部临时表(Using temporary)案例详解及优化解决方法 cookie操作类(加密,获取,删除) MySql 5.7 索引不存在则创建,存在则忽略 SQL SERVER年月周日超止时间 数据抽取的常见理论方法 ETL系列-数据抽取(Extract) 常用时间sql语句 数据库运维:mysql 数据库迁移方法-mysqldump 了解MySQL中的JSON_ARRAYAGG和JSON_OBJECT函数 MySQL的IFNULL()、ISNULL()、NULLIF()函数用法说明 如何看懂explain工具信息,使用explain工具来分析索引 mysql 如何查看sql语句执行时间和效率
MySQL5.7实现row_number()和over()函数
每天进步多一点 · 2026-07-06 · via 博客园 - 每天进步多一点

一、创建测试数据库 db_test

-- 创建测试数据库 db_test
CREATE DATABASE /*!32312 IF NOT EXISTS*/`db_test` /*!40100 DEFAULT CHARACTER SET utf8 */;

二、使用 db_test 数据库

-- 使用 db_test 数据库
USE `db_test`; /*Table structure for table `test1` */

三、先删除测试表看看

-- 先删除测试表看看
DROP TABLE IF EXISTS `test1`;

四、创建测试表

-- 创建测试表
CREATE TABLE `test1` ( 
  `id` int(10) NOT NULL, 
  `score` int(20) DEFAULT NULL, 
  `class` char(10) COLLATE utf8_bin DEFAULT NULL, 
  `name` char(20) COLLATE utf8_bin DEFAULT NULL, 
  PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

五、测试表数据

-- 测试表刷数
/*Data for the table `test1` */
insert into `test1`(`id`,`score`,`class`,`name`) values 
(1,100,'语文','张三'),
(2,98,'语文','李四'),
(3,98,'语文','王五'),
(4,98,'数学','张三'),
(5,96,'数学','李四'),
(6,92,'数学','王五'),
(7,85,'数学','张三'),
(8,96,'语文','张三'),
(9,96,'语文','张三'),
(10,91,'语文','张三'),
(11,91,NULL,'张三');

六、查询测试表

-- 查询测试表
select * from test1;

七、MySQL5.7实现 row_number() over()函数的功能

-- MySQL5.7实现 row_number() over()函数的功能
SELECT id
      ,class
      ,score
      ,rank 
  FROM (SELECT b.*
              -- 定义用户变量 @rownum 来记录数据的行号。通过赋值语句 @rownum := @rownum + 1 来累加达到递增行号。
              ,@rownum := @rownum+1
              -- 如果当前分组编号和上一次分组编号相同,则 @rank (对每一组的数据进行编号)值加1,否则表示为新的分组,从1开始
              ,IF(@pdept = b.class, @rank := @rank + 1, @rank := 1) AS rank
              -- 定义变量 @pdept 用来保存上一次的分组id
              ,@pdept := b.class 
              -- 这里的排序不确定是否需要,保险点还是加上吧
          FROM (SELECT * FROM test1 a ORDER BY a.class, a.score DESC) b 
              -- 初始化自定义变量值
              ,(SELECT @rownum :=0, @pdept := NULL, @rank:=0) c
         -- 该排序必须,否则结果会不对
         ORDER BY b.class, b.score DESC) result
 order by class, rank;