有时会碰到一些需求,查询分组后的最大值,最小值所在的整行记录或者分组后的top n行的记录,在一些别的数据库可能有窗口函数可以方面的查出来,但是MySQL没有这些函数,没有直接的方法可以查出来,可通过以下的方法来查询。

准备工作

测试表结构如下:

root:test> show create table test1\G

*************************** 1. row ***************************

Table: test1

Create Table: CREATE TABLE `test1` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(20) DEFAULT NULL,

`course` varchar(20) DEFAULT NULL,

`score` int(11) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8

1 row in set (0.00 sec)

插入数据:

insert into test1(name,course,score)

values

('张三','语文',80),

('李四','语文',90),

('王五','语文',93),

('张三','数学',77),

('李四','数学',68),

('王五','数学',99),

('张三','英语',90),

('李四','英语',50),

('王五','英语',89);

查看结果:

root:test> select * from test1;

+----+--------+--------+-------+

| id | name   | course | score |

+----+--------+--------+-------+

| 1 | 张三 | 语文 | 80 |

| 2 | 李四 | 语文 | 90 |

| 3 | 王五 | 语文 | 93 |

| 4 | 张三 | 数学 | 77 |

| 5 | 李四 | 数学 | 68 |

| 6 | 王五 | 数学 | 99 |

| 7 | 张三 | 英语 | 90 |

| 8 | 李四 | 英语 | 50 |

| 9 | 王五 | 英语 | 89 |

+----+--------+--------+-------+

TOP 1

查询每门课程分数最高的学生以及成绩

1、使用自连接【推荐】

root:test> select a.name,a.course,a.score from

-> test1 a

-> join (select course,max(score) score from test1 group by course) b

-> on a.course=b.course and a.score=b.score;

+--------+--------+-------+

| name   | course | score |

+--------+--------+-------+

| 王五 | 语文 | 93 |

| 王五 | 数学 | 99 |

| 张三 | 英语 | 90 |

+--------+--------+-------+

3 rows in set (0.00 sec)

2、使用相关子查询

root:test> select name,course,score from test1 a

-> where score=(select max(score) from test1 where a.course=test1.course);

+--------+--------+-------+

| name   | course | score |

+--------+--------+-------+

| 王五 | 语文 | 93 |

| 王五 | 数学 | 99 |

| 张三 | 英语 | 90 |

+--------+--------+-------+

3 rows in set (0.00 sec)

或者

root:test> select name,course,score from test1 a

-> where not exists(select 1 from test1 where a.course=test1.course and a.score < test1.score);

+--------+--------+-------+

| name   | course | score |

+--------+--------+-------+

| 王五 | 语文 | 93 |

| 王五 | 数学 | 99 |

| 张三 | 英语 | 90 |

+--------+--------+-------+

3 rows in set (0.00 sec)

TOP N

N>=1

查询每门课程前两名的学生以及成绩

1、使用union all

如果结果集比较小,可以用程序查询单个分组结果后拼凑,也可以使用union all

root:test> (select name,course,score from test1 where course='语文' order by score desc limit 2)

-> union all

-> (select name,course,score from test1 where course='数学' order by score desc limit 2)

-> union all

-> (select name,course,score from test1 where course='英语' order by score desc limit 2);

+--------+--------+-------+

| name   | course | score |

+--------+--------+-------+

| 王五 | 语文 | 93 |

| 李四 | 语文 | 90 |

| 王五 | 数学 | 99 |

| 张三 | 数学 | 77 |

| 张三 | 英语 | 90 |

| 王五 | 英语 | 89 |

+--------+--------+-------+

6 rows in set (0.01 sec)

2、自身左连接

root:test> select a.name,a.course,a.score

-> from test1 a left join test1 b on a.course=b.course and a.score<b.score

-> group by a.name,a.course,a.score

-> having count(b.id)<2

-> order by a.course,a.score desc;

+--------+--------+-------+

| name   | course | score |

+--------+--------+-------+

| 王五 | 数学 | 99 |

| 张三 | 数学 | 77 |

| 张三 | 英语 | 90 |

| 王五 | 英语 | 89 |

| 王五 | 语文 | 93 |

| 李四 | 语文 | 90 |

+--------+--------+-------+

6 rows in set (0.00 sec)

3、相关子查询

root:test> select *

-> from test1 a

-> where 2>(select count(*) from test1 where course=a.course and score>a.score)

-> order by a.course,a.score desc;

+----+--------+--------+-------+

| id | name   | course | score |

+----+--------+--------+-------+

| 6 | 王五 | 数学 | 99 |

| 4 | 张三 | 数学 | 77 |

| 7 | 张三 | 英语 | 90 |

| 9 | 王五 | 英语 | 89 |

| 3 | 王五 | 语文 | 93 |

| 2 | 李四 | 语文 | 90 |

+----+--------+--------+-------+

6 rows in set (0.01 sec)

4、使用用户变量

root:test> set @num := 0, @course := '';

Query OK, 0 rows affected (0.00 sec)

root:test>

root:test> select name, course, score

-> from (

-> select name, course, score,

-> @num := if(@course = course, @num + 1, 1) as row_number,

-> @course := course as dummy

-> from test1

-> order by course, score desc

-> ) as x where x.row_number <= 2;

+--------+--------+-------+

| name   | course | score |

+--------+--------+-------+

| 王五 | 数学 | 99 |

| 张三 | 数学 | 77 |

| 张三 | 英语 | 90 |

| 王五 | 英语 | 89 |

| 王五 | 语文 | 93 |

| 李四 | 语文 | 90 |

+--------+--------+-------+

6 rows in set (0.00 sec)

MySQL获取分组后的TOP 1和TOP N记录-转的更多相关文章

  1. SQL获取分组后取某字段最大一条记录(求每个类别中最大的值的列表)

    获取分组后取某字段最大一条记录 方法一:(效率最高) select * from test as a where typeindex = (select max(b.typeindex) from t ...

  2. Mysql相关子查询&&MySQL获取分组后的TOP N记录

    小燕子,哈哈哈哈~~~~~~~~~~ 相关子查询是指引用了外部查询列的子查询,即子查询会对外部查询的每行进行一次计算. 举个例子 root:test> show create table tes ...

  3. 【转】Mysql相关子查询&&MySQL获取分组后的TOP N记录

    https://www.cnblogs.com/Yiran583/p/6743870.html select * from test1 a where 2 > (select count(*) ...

  4. 获取分组后的TOP 1和TOP N记录

    MySQL获取分组后的TOP 1和TOP N记录 有时会碰到一些需求,查询分组后的最大值,最小值所在的整行记录或者分组后的top n行的记录,在一些别的数据库可能有窗口函数可以方面的查出来,但是MyS ...

  5. MySQL 对分组后的同类数据进行拼接字符串

    MySQL 对分组后的同类数据进行拼接字符串 写后台方法时遇到个问题,需要将表内同一订单号的操作记录流水进行简单拼接输出,不想取出来再操作,找了个mysql的方法直接操作 //group_concat ...

  6. MySQL 取分组后每组的最新记录

    修改<常用SQL之日期格式化和查询重复数据>中表test1的创建时间,修改后的测试数据如下: 以姓名分组后提取每组最新创建的记录: SELECT a.* FROM test1 AS a, ...

  7. 记一次有意思的 SQL 实现 → 分组后取每组的第一条记录

    开心一刻 今天,朋友气冲冲的走到我面前 朋友:我不是谈了个女朋友,谈了三个月嘛,昨天我偷看她手机,你猜她给我备注什么 我:备注什么? 朋友:舔狗 2 号! 我一听,气就上来了,说道:走,找她去,这婆娘 ...

  8. Mysql 数据分组取某字段值所有最大的记录行

    需求: 表中同一个uid(用户)拥有多条游戏等级记录,现需要取所有用户最高等级(level)的那一条数据,且时间(time)越早排越前.这是典型的排名表 +------+-------+------- ...

  9. Mysql获取去重后的总数

    如果一张表中某个字段存在重复的值,现在我想去重后获取这个字段值的总数 先看这张表 这张表中的openid有重复值 怎么通过sql语句获取openid的去重总数呢 select count(distin ...

随机推荐

  1. Android 组件系列-----Activity生命周期

    本篇随笔将会深入学习Activity,包括如何定义多个Activity,并设置为默认的Activity.如何从一个Activity跳转到另一个Activity,还有就是详细分析Activity的生命周 ...

  2. Redis高可用详解:持久化技术及方案选择

    文章摘自:https://www.cnblogs.com/kismetv/p/9137897.html 前言 在上一篇文章中,介绍了Redis的内存模型,从这篇文章开始,将依次介绍Redis高可用相关 ...

  3. Netty+MUI从零打造一个仿微信的高性能聊天项目,兼容iPhone/iPad/安卓

    要说到微信,我相信是个人都应该知道,几乎人人都会安装这款社交APP吧,它已经成为了我们生活中不可缺少的一份子. 我记得我上大学那会刚接触Java,做的第一个小项目就是基于J2SE的聊天室,使用Java ...

  4. 菜鸟教程之工具使用(八)——EGit禁止自动转换回车换行符

    众所周知,Windows和Linux系统的回车换行是不一样的.想要进一步了解它们的可以阅读下面的介绍,不感兴趣的可以直接跳过. 产生背景 关于“回车”(carriage return)和“换行”(li ...

  5. 【MQTT】Mosquitto的安装与使用流水记

    最近使用MQTT,安装Mosquitto试一下,并记录下来. 软件准备 从官网获取安装包: wget http://mosquitto.org/files/source/mosquitto-1.4.1 ...

  6. git-ftp代码部署方式

    虽然如今ci方法已经在很多团队使用了,但对于一些个人性的基于PHP的跑在虚拟主机的小项目,既没有服务端的Git环境,又不想时刻跑一个Genkins,就只能回到原始的FTP上传了. 所幸有了git-ft ...

  7. [echarts] 横纵数据散点图

    需求:课程平均分(X)与课程通过率散点图 http://echarts.baidu.com/echarts2/doc/example/scatter1.html https://www.cnblogs ...

  8. Redis系统性介绍

    虽然Redis已经很火了,相信还是有很多同学对Redis只是有所听闻或者了解并不全面,下面是一个比较系统的Redis介绍,对Redis的特性及各种数据类型及操作进行了介绍.是一个很不错的Redis入门 ...

  9. 一个Login页面全面了解session与cookie

    背景 做了四年的前端开发,对外一直说自己是web开发,那么身为一个web开发怎能不知道session与cookie以及其管理方式呢~ Login涉及技术栈:Nodejs,MongoDB,Express ...

  10. 如何处理MySQL每月5亿的数据

    第一阶段:1,一定要正确设计索引2,一定要避免SQL语句全表扫描,所以SQL一定要走索引(如:一切的 > < != 等等之类的写法都会导致全表扫描)3,一定要避免 limit 100000 ...