MySQL5.7 排序

devtools/2024/9/24 7:16:25/

一、不分组排序
(1).排序-并列数据随机顺序

select
col1,
col2,
@rank := @rank + 1 as rank 
from (select 'A' as col1,100 as col2union all select 'B' as col1,130 as col2union all select 'C' as col1,120 as col2union all select 'D' as col1,120 as col2order by col2 desc
) a,(select @rank:=0) b;

(2).排序-并列数据排名相同且连续

select
a.col1,
a.col2,
if( @col2 = a.col2, @rank, @rank := @rank + 1 ) as rank,
@col2 := a.col2
from (select 'A' as col1,100 as col2union all select 'B' as col1,130 as col2union all select 'C' as col1,120 as col2union all select 'D' as col1,120 as col2order by col2 desc
) a,(select @rank:=0, @col2 := NULL) b;

(3).排序-并列数据排名相同且不连续

select
a.col1,
a.col2,
@rownum := @rownum + 1,
if( @col2 = a.col2, @rank, @rank := @rownum) as rank,
@col2 := a.col2
from (select 'A' as col1,100 as col2union all select 'B' as col1,130 as col2union all select 'C' as col1,120 as col2union all select 'D' as col1,120 as col2order by col2 desc
) a,(select @rownum:=0,@rank:=0,@col2 := NULL) b;

二、分组排序
(1).分组排序-并列数据随机顺序

select 
c.col1,
c.col2,
c.rank
from (selecta.col1,a.col2,@rownum:=@rownum+1,if(@col1=a.col1,@rank:=@rank+1,@rank:=1) as rank,@col1:=a.col1from (select 'A' as col1,100 as col2union all select 'A' as col1,130 as col2union all select 'B' as col1,120 as col2union all select 'B' as col1,110 as col2order by col1,col2 desc) a,(select @rownum:=0,@col1:=null,@rank:=0) b
) c ;

(2).分组排名 -并列数据排名连续


```dart
select 
c.col1,
c.col2,
c.rank
from (selectobj.col1,obj.col2,IF(@col1 = col1, IF( @col2 = obj.col2, @rownum, @rownum := @rownum+1 ),@rownum :=1) as rank,@col1 := obj.col1,@col2 := obj.col2from (select 'A' as col1,100 as col2union all select 'A' as col1,130 as col2union all select 'A' as col1,130 as col2union all select 'B' as col1,120 as col2union all select 'B' as col1,110 as col2order by col1,col2 desc) as obj,(select @rownum := 0,@col1:=null,@col2 := null) r
) c;

(3).分组并列排名 -并列数据排名不连续```dart
select 
c.col1,
c.col2,
c.rank
from (selectobj.col1,obj.col2,if(@col1 = obj.col1, @rownum := @rownum + 1, @rownum :=1),if(@col1 = obj.col1, if(@col2 = obj.col2, @rank, @rank := @rownum ),@rank :=1) as rank,@col1 := obj.col1,@col2 := obj.col2from (select 'A' as col1,100 as col2union all select 'A' as col1,130 as col2union all select 'A' as col1,130 as col2union all select 'B' as col1,120 as col2union all select 'B' as col1,110 as col2order by col1,col2 desc) as obj,(select @rownum := 0,@rank := 0,@col1:=null,@col2 := null) r
) c;

三、指定顺序排序

select * 
from (select 1 as idunion all select 2 as idunion all select 3 as idunion all select 4 as idunion all select 5 as idunion all select 6 as idunion all select 7 as id
) a
where `id` IN (1, 7, 3, 5) 
order by FIELD(`id`, 5, 3, 7, 1);

http://www.ppmy.cn/devtools/87961.html

相关文章

Git进阶(十五):不同Git仓库设置不同配置项

文章目录 一、前言二、项目配置三、全局配置四、拓展阅读 一、前言 项目源码管理阶段,会遇到不同项目设置不同用户信息等场景。 为不同的Git仓库设置不同的配置项,需要在仓库的本地配置文件中进行设置,而不是使用全局或系统级别的配置。Git…

文件解析漏洞总结(IIS,NGINX,APACHE)

目录 一、IIS解析漏洞 IIS6.X 方式一:目录解析 方式二:畸形文件解析 IIS7.X 利用条件 环境配置 下载链接: 二、Nginx解析漏洞 2.1:nginx_parsing 利用条件 利用姿势 2.2:CVE-2013-4547 影响版本 利用姿势…

手写RPC框架,与Spring整合,基于Netty作为网络框架,protobuf作为序列化协议。可以和实际项目相结合完美运行

注:由于RPC框架过于庞大所以本篇文章只是作为阅读RPC源码的一个指导,设计精巧之处还需要各位读者结合源码进行实践 RPC源码地址:https://github.com/xhpcd/rpc git clone: https://github.com/xhpcd/rpc.git 如果觉得有收获麻烦留下一颗st…

学会网络安全:开启广阔职业与责任之旅

在数字化时代,网络安全已成为社会经济发展的重要基石。随着互联网的普及和技术的飞速发展,网络安全威胁日益复杂多变,对国家安全、社会稳定以及个人隐私构成了严峻挑战。因此,掌握网络安全技能不仅意味着拥有了一项高价值的职业技…

【小知识】站在前人的肩膀上写程序——STL库初阶算法函数的使用

【小知识】站在前人的肩膀上写程序——STL库初阶算法函数的使用 1.墨水瓶算法和swap函数2.打擂台算法和max,min函数3.排序——sort函数 1.墨水瓶算法和swap函数 如果想交换两个墨水瓶的墨水该怎么办呢?我们可以准备第三个墨水瓶。将第一个墨水瓶的墨水倒…

Docker镜像仓库

目录 前言 1. 常见的镜像仓库 2. 搭建私有镜像仓库 3. 私有库的推送、拉取镜像 4. 总结 前言 Docker镜像仓库简单来说就是存储和管理Docker镜像的平台或服务。它允许开发人员上传自己创建的镜像,并与团队成员共享和协作使用。 1. 常见的镜像仓库 镜像仓库有公…

PADS Router 扇出失败问题详细解决方法。

第一步:确定单位是一致的,我的单位是 “密尔”,不是“公制”。 第二步:进去pads router 右键选择特性,注意,是右键点击任意板框内空白位置的特性,这个是涵盖整体的设置,和单独点击一个元器件选…

基于docker的 nacos安装部署

一、拉取镜像 拉取nacos官方镜像,这里使用默认命令 docker pull nacos/nacos-server二、创建挂载目录 创建本地的映射文件application.properties mkdir -p /home/docker/nacos/conf /home/docker/nacos/logstouch /home/docker/nacos/conf/application.propert…