有时会碰到一些需求,查询分组后的最大值,最小值所在的整行记录或者分组后的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. C#通过DSOFile读取与修改文件的属性

    搜了一圈用C#读取与修改文件属性的文章,结果几乎找不到- -: 偶然间看到一个DSOFile工具,然后找到了对该工具进行详细讲解的一篇文章:<DSOfile,一个修改windows系统文件摘要的 ...

  2. ubuntu安装odbc及(mysql驱动)

    一.安装odbc apt-get install unixodbc 如果需要用到编译的头文件之类的 apt-get install unixodbc-dev 二.安装mysql驱动 apt-get i ...

  3. Ansible 使用普通用户远程执行playbook

    设置ansible使用普通用户jsxge远程连接执行playbook 1. ansible控制端创建普通用户jsxgecd /homeuseradd jsxgechown -R jsxge.wheel ...

  4. zabbix server is not running,the information dispalyed may not be current

    查看zabbix服务器和客户端的端口及进程都是正常启动,打印的日志也没什么异常,但是就是在主页提示zabbix server is not running 不防尝试改一下zabbix_server的配 ...

  5. Atitit  技术经理职责与流程表总结

    Atitit  技术经理职责与流程表总结 1. (最重要) 理念 价值观建设  ***团队文化建设2 1.1. 加强跨项目组员沟通 ,防止重复劳动2 1.2. 活动聚餐2 2. (重要)方向建设 技术 ...

  6. IOS SDK -UITableView的奇葩特性

    UITableView是IOS提供的一个带有复用机制的滚动表格,目前的基本功能就是垂直的表格,可以有多个section,每个section可以有多个row,然后还包含有sectionview,foot ...

  7. FragmentPagerAdapter 与 FragmentStatePagerAdapter 的区别

    参考链接: http://blog.csdn.net/dreamzml/article/details/9951577 简单来说前者适合静态.少量的Fragment 后者适合动态.较多的Fragmen ...

  8. 【iCore4 双核心板_ARM】例程十六:USB_HID实验——双向数据传输

    实验方法: 1.USB_HID协议免驱动,此例程不需要驱. 2.将跳线冒跳至USB_OTG,通过Micro USB 线将iCore4 USB-OTG接口与电脑相连. 3.打开上位机软件usb_hid. ...

  9. 【iCore4 双核心板_ARM】例程二十一:LWIP_TCP_SERVER实验——以太网数据传输

    实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...

  10. ubuntu下使用golang、qml与ubuntu sdk开发桌面应用 (简单示例)

    找了很长时间go的gui库,试了gtk,准备试qt的时候发现了这个qml库,试了下很好用. ##准备工作 **1.Go 1.2RC1** go的版本应该不能低于这个,我是在1.2RC发布当天升级后发现 ...