博客
关于我
mysql中还有窗口函数?这是什么东西?
阅读量:791 次
发布时间:2023-02-11

本文共 2201 字,大约阅读时间需要 7 分钟。

图片

什么是窗口函数?

mysql8.0的版本中,新增了一个窗口函数,用他可以实现很多新的查询方式。窗口函数类似于sun()count()那样的集合函数,但它并不会将多行查询结果合并为一行,而是将结果放回多行中。什么意思呢?就是说窗口函数是不需要group by的。

窗口函数-排名

首先我们创建一个名字为test_2的数据表;

mysql> create table test_2 (name char(200) not null, brcount int(20) not null);Query OK, 0 rows affected, 1 warning (0.02 sec)mysql>

然后插入几条数据;

mysql> insert into test_2 values ('test1', 10), ('test2', 14), ('test3', 8), ('test4', 99), ('test5', 3);Query OK, 5 rows affected (0.01 sec)Records: 5  Duplicates: 0  Warnings: 0mysql>

查看下表中数据:

mysql> select * from test_2;+-------+---------+| name  | brcount |+-------+---------+| test1 |      10 || test2 |      14 || test3 |       8 || test4 |      99 || test5 |       3 |+-------+---------+5 rows in set (0.00 sec)mysql>

按照字段brcount从小到大来进行排序,可以利用窗口函数来实现;

mysql> select *, rank() over w1 as 'rand' from test_2 window w1 as (order by brcount);+-------+---------+------+| name  | brcount | rand |+-------+---------+------+| test5 |       3 |    1 || test3 |       8 |    2 || test1 |      10 |    3 || test2 |      14 |    4 || test4 |      99 |    5 |+-------+---------+------+5 rows in set (0.01 sec)mysql>

这里创建了名称为w1的窗口函数,规定对brcount字段进行排序,然后在select子句中对窗口函数w1执行rank()方法,将结果输出为rank字段。

RANK()函数为结果集的分区中的每一行分配一个排名。行的等级由一加上前面的等级数指定。

需要注意的是,在这里的windows w1是可选的,如下:

mysql> select *, rank() over w1 as 'rand' from test_2 window w1 as (order by brcount);+-------+---------+------+| name  | brcount | rand |+-------+---------+------+| test5 |       3 |    1 || test3 |       8 |    2 || test1 |      10 |    3 || test2 |      14 |    4 || test4 |      99 |    5 |+-------+---------+------+5 rows in set (0.00 sec)mysql>

亦或者查一下各自所占百分比;

mysql> select *, (brcount)/(sum(brcount) over()) as rate from test_2;+-------+---------+--------+| name  | brcount | rate   |+-------+---------+--------+| test1 |      10 | 0.0746 || test2 |      14 | 0.1045 || test3 |       8 | 0.0597 || test4 |      99 | 0.7388 || test5 |       3 | 0.0224 |+-------+---------+--------+5 rows in set (0.00 sec)mysql>

至此,本文结束。

更多内容请转至VX公众号 “运维家” ,获取最新文章。

------ “运维家” ------

------ “运维家” ------

------ “运维家” ------

临武县运维工程师培训,温州运维工程师,通达oa实施运维工程师,呼叫中心运维工程师面试

腾讯idc运 维工程师面试,运维工程师samba,运维工程师的行业是什么,
运维工程师的来历,运维工程师好找嘛,运维 工程师出入,运维工程师自学可行

转载地址:http://dtbfk.baihongyu.com/

你可能感兴趣的文章
mt_rand
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>