当前位置: 首页 > news >正文

MySQL5.7 排序

一、不分组排序
(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.lryc.cn/news/412649.html

相关文章:

  • 【Unity】3D功能开发入门系列(一)
  • 【Python体验】第五天:目录搜索、数据爬虫(评论区里写作业)
  • elasticsearch性能调优方法原理与实战
  • python print 函数参数:sep 自定义分隔符,end 自定义结尾符
  • git 使用场景
  • Ubuntu22.04 Docker更换阿里云镜像
  • Windows下Rust OpenCV环境配置
  • PostgreSQL(二十三)TOAST技术
  • 文章解读与仿真程序复现思路——电力系统自动化EI\CSCD\北大核心《海上风氢系统与沿海电网能量协同优化调度》
  • MySQL update set语句中 逗号与and的区别
  • C++面试---小米
  • Java 实现 AVL树
  • CNN卷积网络实现MNIST数据集手写数字识别
  • 深入理解Java中的时间处理与时区管理
  • 虚拟机windows server创建域
  • Java 集合框架:Java 中的 Set 集合(HashSet LinkedHashSet TreeSet)特点与实现解析
  • springboot智能健康管理平台-计算机毕业设计源码57256
  • LetterBox图像预处理方法
  • C++第五篇 类和对象(下) 初始化列表
  • C#中的通信
  • CVE-2022-21663: WordPress <5.8.3 版本对象注入漏洞深入分析
  • C语言笔试题(三)
  • minio笔记之windows下安装使用
  • 代码随想录算法训练营day31 | 56. 合并区间、738.单调递增的数字
  • 利用 Python 制作图片轮播应用
  • 报表系统之Cube.js
  • 代码随想录算法训练营第45天
  • solidity合约创建
  • 队列---循环队列实现
  • 【视频讲解】后端增删改查接口有什么用?