mysql特殊查询----分组后排序
使用的示例表
学生表----student
表结构

数据

查询方法
一、第一种方法
我认为这是比较传统,比较容易理解的一种方式,使用自连接,并在连接条件中作比较,之后再对查询条件分组统计,排序。
select a.id,a.class,a.source
from student a left join student b on a.class=b.class and a.source<=b.source
group by a.class,a.source
order by a.class,a.source
结果:

分析一下查询过程:
1、自连接并使用比较条件
select a.id,a.class,a.source asource,b.source bsource
from student a left join student b on a.class=b.class and a.source<=b.source
order by a.class,a.source
查询结果:


以上查询数据可以看出,每个班等于或比每一个asource分数多bsource有几条数据,最终,最小的asource会有一个班级人数的数据条数,最大的asource只会有一条数据。这就是排序的依据。
之后,对数据分组。
2、对数据进行分组
select a.id,a.class,a.source asource,count(a.source)
from student a left join student b on a.class=b.class and a.source<=b.source
group by a.class,a.source
order by a.class,a.source
查询结果:

以上数据已经可以直观的看出数据被排序后的结果。
3、对分组后的数据截取前N条或后N条
a、首先是保留前N条数据,使用having。
select a.id,a.class,a.source asource,count(a.source)
from student a left join student b on a.class=b.class and a.source<=b.source
group by a.class,a.source
having count(a.source)<=
order by a.class,a.source
查询结果:

这样截取到的每个班最高的三个分数,因为比较条件是:a.source<=b.source,导致asource最大的分数只有一条,最小的有最多的条数,所以在使用having获得统计数最少的三条数据时,会得到三个最高分。
如果要得到最低的三个分数的数据,就要保证最小的分数有最少的数据(不一定是一条,当有两个最高分)。因为a.source<=b.source比较条件是相互的,所以只需要将asource参与的查询换成bsource,就可以实现获得最少分数的需求。
b、获得后N条数据
select a.id,a.class,b.source bsource,count(b.source)
from student a left join student b on a.class=b.class and a.source<=b.source
group by a.class,b.source
having count(b.source)<=3
order by a.class,b.source
查询结果:

二、第二种方法-----只用于分组排序后取前N条数据
1、分组后取前N或后N条,关键在于比较,通过where俩控制查询条数
最大的前3条数据
select * from student as a
where 3>(select count(*) from student where class=a.class and source>a.source)
ORDER BY class ,source desc
查询结果:

最小的前3条数据
select * from student as a
where 3>(select count(*) from student where class=a.class and source<a.source)
ORDER BY class ,source
查询结果:

这种查询方式,某些逻辑无法理解。
mysql特殊查询----分组后排序的更多相关文章
- 排序(分组后排序&整排)
一.整排 要求:根据score进行排名,分数相同,名次相同,且连续 表如下图: sql语句: 方法一:select a.score, (select count(distinct b.score) f ...
- [MySQL]MySQL数据库中如何查询分组后每组中的最后一条记录?
原文地址:https://codedefault.com/s/how-can-i-retrieve-the-last-record-in-each-group-mysql 问题描述 比如,在MySQL ...
- MySQL 排名、分组后组内排名、取各组的前几名 及排名后更新插入数据表中
一.排名 /*普通排名:从1开始,顺序往下排*/ AS rank ) r ORDER BY score; /*并列排名:相同的值是相同的排名*/ SELECT cs.* , CASE WHEN @p= ...
- MySQL给临时表分组后Max函数无效
有道练习题"取得平均薪水最高的部门的部门编号(至少给出两种解决方案)", 为什么我给临时表分组后Max函数就无效了?不分组就可以,但是无法查询到DEPTNO,MySQL版本8.0+ ...
- group by 查询分组后 组的条数
比如select gid from table group by gid 查询时使用下面的方法查询条数 select count(distinct gid) from table 使用select c ...
- nodejs操作 mongoose(mongodb)和Sequelize(mysql)查询数据后添加新属性未生效
最近在着手koa时候,发现mongoose(mongodb)查询数据库后添加新属性,前端拿不到新属性问题, 然后测试了一下Sequelize(mysql),发现也有同样的问题存在.此时着手干! 1.1 ...
- ObservableCollection 分组后排序报错问题
ObservableCollection通过Move方法可以移动顺序,如下: 将ObservableCollection中的一个item置顶: private ObservableCollection ...
- MySQL 排名、分组后组内排名、取各组的前几名
一.排名 /*普通排名:从1开始,顺序往下排*/ AS rank ) r ORDER BY score; /*并列排名:相同的值是相同的排名*/ SELECT cs.* , CASE WHEN @p= ...
- C# MongoDB 查询,分组,聚合,排序,条件,分页
先下载个C#的驱动.MongoDB提供各种主流与非主流预言的开发驱动. C# Driver 下载地址:这里 CSharp Driver Tutorial:这里 下载文件安装或者解压缩包 如果您是安装, ...
随机推荐
- LinkedBlockingQueue 注记
近期看一个音频传输代码时,对方采用了LinkedBlockingQueue为生产者.消费者模式,来支撑读写线程. 个人感觉非常不错,因此也对这种方式进行总结,并梳理了一个基本的功能框架备用.主要两点: ...
- [Swift]LeetCode410. 分割数组的最大值 | Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [Swift]LeetCode637. 二叉树的层平均值 | Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...
- [Swift]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal
Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this b ...
- iOS学习—— UISearchBar的使用
转载自:http://blog.sina.com.cn/s/blog_7b9d64af0101dfg8.html 最近用到搜索功能.于是,经过不断的研究,终于,有点懂了. 那就来总结一下吧,好记性不如 ...
- Python 创建递归文件夹
# 创建递归文件夹 def createfiles(filepathname): try: os.makedirs(filepathname) except Exception as err: pri ...
- JVM基础系列第3讲:到底什么是虚拟机?
我们都知道在 Windows 系统上一个软件包装包是 exe 后缀的,而这个软件包在苹果的 Mac OSX 系统上是无法安装的.类似地,Mac OSX 系统上软件安装包则是 dmg 后缀,同样无法在 ...
- ASP.NET在MVC控制器中获取Form表单值的方法
在网站开发中我们经常需要用到表单,那么,在前台页面的表单中提交到后台控制器后,后台控制器如何接收表单提交过来的数据呢?下面我们介绍几种常用的方法. 我们先看看前台页面,这里我们用一个用户名和密码的表单 ...
- Qt之QComboBox定制
说起下拉框,想必大家都比较熟悉,在我们注册一些网站的时候,会出现大量的地区数据供我们选择,这个时候出现的就是下拉框列表,再比如字体选择的时候也是使用的下拉框,如图1所示.下拉框到处可见,作为一个图形库 ...
- ASP.NET Core WebAPI中的分析工具MiniProfiler
介绍 作为一个开发人员,你知道如何分析自己开发的Api性能么? 在Visual Studio和Azure中, 我们可以使用Application Insight来监控项目.除此之外我们还可以使用一个免 ...