有时会碰到一些需求,查询分组后的最大值,最小值所在的整行记录或者分组后的top n行的记录,像在hive中是有窗口函数的,可以通过它们来实现,但是MySQL没有这些函数,可通过下面的方法来实现

1、准备

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 insert into test1(name,course,score)
values
('张三','语文',80),
('李四','语文',90),
('王五','语文',93),
('张三','数学',77),
('李四','数学',68),
('王五','数学',99),
('张三','英语',90),
('李四','英语',50),
('王五','英语',89);

2、TOP 1

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

实现方法:可以通过自连接、子查询来实现,如下

a、自连接实现

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;

 执行效果如下

b、子查询实现

select name,course,score
from test1 a
where score=(select max(score) from test1 where a.course=test1.course);

执行效果如下

也可以用下面这个子查询实现

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

执行效果如下

3、TOP N

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

实现方式:使用union all、自身左连接、子查询、用户变量等方式实现

a、使用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)
union all
(select name,course,score from test1 where course='英语' order by score desc limit 2);

执行效果如下

b、使用自身左连接

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;

执行效果如下

c、使用子查询

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;

执行效果如下

d、使用用户变量

set @num := 0, @course := '';

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;

执行效果如下

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】。
如果,您希望更容易地发现我的新博客,不妨点击一下左下角的【关注我】。
如果,您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客,我是【刘超★ljc】。

本文版权归作者,禁止转载,否则保留追究法律责任的权利。

mysql实现topN top1的更多相关文章

  1. 数据库之MySQL的介绍与使用20180703

    /*******************************************************************************************/ 一.mysq ...

  2. 【mysql】一维数据TopN的趋势图

    创建数据表语句 数据表数据 对上述数据进行TopN排名 select severity,sum(count) as sum from widgt_23 where insertTstamp>=' ...

  3. mysql 实现某年单季度内的品牌TOPn销量在此年此单季度内销量占比

    数据表:       结果表: mysql语句:  

  4. mysql分组取topn

    本文来自  http://www.jb51.net/article/31590.htm 有些语句sql top n 是sqlserver语法 --按某一字段分组取最大(小)值所在行的数据 代码如下: ...

  5. 大数据学习——mapreduce学习topN问题

    求每一个订单中成交金额最大的那一笔  top1 数据 Order_0000001,Pdt_01,222.8 Order_0000001,Pdt_05,25.8 Order_0000002,Pdt_05 ...

  6. mysql索引设计的注意事项

    mysql索引设计的注意事项 目录 一.索引的重要性 二.执行计划上的重要关注点 (1).全表扫描,检索行数 (2).key,using index(覆盖索引) (3).通过key_len确定究竟使用 ...

  7. mysql索引设计的注意事项(大量示例,收藏再看)

    mysql索引设计的注意事项(大量示例,收藏再看) 目录 一.索引的重要性 二.执行计划上的重要关注点 (1).全表扫描,检索行数 (2).key,using index(覆盖索引) (3).通过ke ...

  8. 大数据项目实践:基于hadoop+spark+mongodb+mysql+c#开发医院临床知识库系统

    一.前言 从20世纪90年代数字化医院概念提出到至今的20多年时间,数字化医院(Digital Hospital)在国内各大医院飞速的普及推广发展,并取得骄人成绩.不但有数字化医院管理信息系统(HIS ...

  9. MySql学习(六) —— 数据库优化理论(二) —— 查询优化技术

    逻辑查询优化包括的技术 1)子查询优化  2)视图重写  3)等价谓词重写  4)条件简化  5)外连接消除  6)嵌套连接消除  7)连接消除  8)语义优化 9)非SPJ优化 一.子查询优化 1. ...

随机推荐

  1. C#转义字符[转]

    C#转义字符: 一种特殊的字符常量 以反斜线"\"开头,后跟一个或几个字符 具有特定的含义,不同于字符原有的意义,故称“转义”字符. 主要用来表示那些用一般字符不便于表示的控制代码 ...

  2. 浅析ActiveReport中数据下拉列表的交互性

    虽说做Cognos已经很久了,Cognos的active report还很少开发过,于是便做了一些小的尝试,下面就以具体实例来分析一下在report studio的活动报表中数据下拉列表和列表报表以及 ...

  3. 反编译示例:mxd检查

    gisoralce在博客园发布了一个mxd检查工具,主要功能是将arcgis的mxd数据源有效性(含矢量和影像)检查.检查是否为相对路径,自动保存为相对路径. 这是一个未加壳的.NET程序,正好拿来练 ...

  4. UNIX网络编程读书笔记:TCP输出、UDP输出和SCTP输出

    TCP输出 下图展示了应用进程写数据到TCP套接口的过程. 每一个TCP套接口有一个发送缓冲区,我们可以用SO_SNDBUF套接口选项来改变这个缓冲区的大小. 当应用进程调用write时,内核从应用进 ...

  5. Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  6. 转:不在同一个服务器上的数据库之间的数据操作(oracle/sql server的对比)

    如何操做不在同一个数据库中的数据操作: 一.对于SQL server来讲:  1.采用创建链接服务器的方式:    (1).创建链接服务器       exec sp_addlinkedserver  ...

  7. Linux 监测常用的图形工具

    cacti zabbix nagios nagiosgraph

  8. smartsvn9破解及license文件

    第一步:去官网下载自己系统smartsvn版本文件 下载地址:http://www.smartsvn.com/download 第二步:破解 (1) 将文件解压到系统路径:/opt/smartsvn ...

  9. ACE中TASK架构简介及简单应用

    一.基础功能介绍 1.ACE_Message_Block*,Windows消息用MSG结构表示,ACE_Task中因为不能预计各种应用中消息的类型,所以ACE_Message_Block基本上可以理解 ...

  10. sae python中Mysql中文乱码的解决

    一開始我用的是: db=MySQLdb.connect(db=sae.const.MYSQL_DB,user=sae.const.MYSQL_USER,passwd=sae.const.MYSQL_P ...