使用的示例表

学生表----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特殊查询----分组后排序的更多相关文章

  1. 排序(分组后排序&整排)

    一.整排 要求:根据score进行排名,分数相同,名次相同,且连续 表如下图: sql语句: 方法一:select a.score, (select count(distinct b.score) f ...

  2. [MySQL]MySQL数据库中如何查询分组后每组中的最后一条记录?

    原文地址:https://codedefault.com/s/how-can-i-retrieve-the-last-record-in-each-group-mysql 问题描述 比如,在MySQL ...

  3. MySQL 排名、分组后组内排名、取各组的前几名 及排名后更新插入数据表中

    一.排名 /*普通排名:从1开始,顺序往下排*/ AS rank ) r ORDER BY score; /*并列排名:相同的值是相同的排名*/ SELECT cs.* , CASE WHEN @p= ...

  4. MySQL给临时表分组后Max函数无效

    有道练习题"取得平均薪水最高的部门的部门编号(至少给出两种解决方案)", 为什么我给临时表分组后Max函数就无效了?不分组就可以,但是无法查询到DEPTNO,MySQL版本8.0+ ...

  5. group by 查询分组后 组的条数

    比如select gid from table group by gid 查询时使用下面的方法查询条数 select count(distinct gid) from table 使用select c ...

  6. nodejs操作 mongoose(mongodb)和Sequelize(mysql)查询数据后添加新属性未生效

    最近在着手koa时候,发现mongoose(mongodb)查询数据库后添加新属性,前端拿不到新属性问题, 然后测试了一下Sequelize(mysql),发现也有同样的问题存在.此时着手干! 1.1 ...

  7. ObservableCollection 分组后排序报错问题

    ObservableCollection通过Move方法可以移动顺序,如下: 将ObservableCollection中的一个item置顶: private ObservableCollection ...

  8. MySQL 排名、分组后组内排名、取各组的前几名

    一.排名 /*普通排名:从1开始,顺序往下排*/ AS rank ) r ORDER BY score; /*并列排名:相同的值是相同的排名*/ SELECT cs.* , CASE WHEN @p= ...

  9. C# MongoDB 查询,分组,聚合,排序,条件,分页

    先下载个C#的驱动.MongoDB提供各种主流与非主流预言的开发驱动. C# Driver 下载地址:这里 CSharp Driver Tutorial:这里 下载文件安装或者解压缩包 如果您是安装, ...

随机推荐

  1. CESSNA: Resilient Edge-Computing

    CESSNA: 弹性边缘计算 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时间仓促,且笔者英 ...

  2. Mesos源码分析(15): Test Executor的运行

    Test Executor的代码在src/examples/test_executor.cpp中   int main(int argc, char** argv) {   TestExecutor ...

  3. 【从零开始搭建自己的.NET Core Api框架】(一)创建项目并集成swagger:1.1 创建

    系列目录 一.  创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...

  4. [Swift]LeetCode488. 祖玛游戏 | Zuma Game

    Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), gre ...

  5. [Swift]LeetCode715. Range 模块 | Range Module

    A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...

  6. [Swift]LeetCode810. 黑板异或游戏 | Chalkboard XOR Game

    We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take tu ...

  7. [Swift]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin

    We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the d ...

  8. 【offer收割机必备】我简历上的Java项目都好low,怎么办?

    这篇文章我们来聊一聊,在系统设计和项目经验这两块,应该如何充分的准备,才能拿出有技术含量的项目经验战胜跟你同台竞技的其他工程师,征服你的面试官,收获各种心仪的offer. (1)高级工程师必备:系统设 ...

  9. 设计模式的征途—4.抽象工厂(Abstract Factory)模式

    上一篇的工厂方法模式引入了工厂等级结构,解决了在原来简单工厂模式中工厂类职责太重的原则,但是由于工厂方法模式的每个工厂只生产一类产品,可能会导致系统中存在大量的工厂类,从而增加系统开销.那么,我们应该 ...

  10. Unity资源打包学习笔记(一)、详解AssetBundle的流程

    转载请标明出处:http://www.cnblogs.com/zblade/ 本文参照unity官网上对于assetBundle的一系列讲解,主要针对assetbundle的知识点做一个梳理笔记,也为 ...