mysql GROUP_CONCAT获取分组的前几名
比如说要获取班级的前3名,oracle 可以用 over partition by 来做。mysql就可以用GROUP_CONCAT + GROUP BY + substring_index实现。
考试表
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`score` int(11) DEFAULT NULL,
`class` char(12) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入数据
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('1', 'Bobdd', '25', '1');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('2', 'xx', '20', '2');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('3', 'Jack', '30', '2');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('4', 'Bill', '32', '4');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('5', 'Nick', '22', '3');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('6', 'Kathy', '18', '3');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('7', 'Steve', '36', '3');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('8', 'Anne', '25', '2');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('9', 'Kathy', '18', '2');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('11', 'Bob1', '25', '3');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('12', 'Jane1', '20', '1');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('13', 'Jack1', '30', '1');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('14', 'Bill1', '32', '1');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('15', 'Nick1', '22', '4');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('16', 'Kathy1', '18', '4');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('17', 'Steve1', '36', '4');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('18', 'Anne1', '25', '1');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('19', 'Kathy1', '18', '2');
运用group_concat + GROUP BY 分组 获取前3名
select GROUP_CONCAT(t1.id) as ids from (
SELECT t.class, substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',3) as id from
test t GROUP BY t.class
)t1
得到

注意 是t.id ORDER BY t.score desc 分数从高到低。
上面的语句只是获取到总的id。但是转换为列不太好弄。可以拆分用union all 来搞。
获取第一名
SELECT t.class, substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',1) as id from
test t GROUP BY t.class
union all
-- 第二名
SELECT t.class, substring_index(substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',2),',',-1) as id from
test t GROUP BY t.class
union all
-- 第三名
SELECT t.class, substring_index(substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',3),',',-1) as id from
test t GROUP BY t.class
好了到现在 已经获取到了一个list
用 in 来完成最后的步骤
SELECT class,score,name FROM test where id in(
SELECT id from
(SELECT t.class, substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',1) as id from
test t GROUP BY t.class
union all
SELECT t.class, substring_index(substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',2),',',-1) as id from
test t GROUP BY t.class
union all
SELECT t.class, substring_index(substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',3),',',-1) as id from
test t GROUP BY t.class) t2
) ORDER BY class asc,score desc
最终结果

mysql GROUP_CONCAT获取分组的前几名的更多相关文章
- mysql GROUP_CONCAT+ GROUP BY + substring_index获取分组的前几名
mysql方法来源于:http://www.cnblogs.com/jjcc/p/5896588.html ###在网上看到一篇,非常赞的方法### 比如说要获取班级的前3名,mysql就可以用GRO ...
- mysql 查询每个分组的前几名
按分组排序,并查出每个分组的前3名 单表 SELECT * FROM ( SELECT ZONEID, uid, NAME, fight, IF ( , ) AS rank, ( @zone := z ...
- 如何在MySQL中查询每个分组的前几名【转】
问题 在工作中常会遇到将数据分组排序的问题,如在考试成绩中,找出每个班级的前五名等. 在orcale等数据库中可以使用partition语句来解决,但在mysql中就比较麻烦了.这次翻译的文章就是专门 ...
- 如何在mysql中查询每个分组的前几名
问题 在工作中常会遇到将数据分组排序的问题,如在考试成绩中,找出每个班级的前五名等. 在orcale等数据库中可以使用partition 语句来解决,但在MySQL中就比较麻烦了.这次翻译的文章就是 ...
- MySQL 排名、分组后组内排名、取各组的前几名 及排名后更新插入数据表中
一.排名 /*普通排名:从1开始,顺序往下排*/ AS rank ) r ORDER BY score; /*并列排名:相同的值是相同的排名*/ SELECT cs.* , CASE WHEN @p= ...
- MySQL 排名、分组后组内排名、取各组的前几名
一.排名 /*普通排名:从1开始,顺序往下排*/ AS rank ) r ORDER BY score; /*并列排名:相同的值是相同的排名*/ SELECT cs.* , CASE WHEN @p= ...
- mysql查询每个部门/班级前几名
Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id . +----+-------+--------+--------------+ | I ...
- mysql 查询每个班级成绩前两名
- SQL Server 2008 R2——分组取前几名
=================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载 请通过右 ...
随机推荐
- 结构体的vector resize()与初始化
序: 我们在使用vector的时候可以自定义里面的数据类型.例如这样: struct Edge{ int from; int to; int weight; }; vector<Edge> ...
- IOS开发常用的基础方法
.//退出键盘 [self.view endEditing:YES]; 隐藏手机上方的状态栏 -(BOOL)prefersStatusBarHidden{ return YES; } //获取当前控制 ...
- React Native技术做的一个项目“微笑阅读”
最近用React Native做了一个APP应用,有点心得: React Native确实比Hybrid应用渲染快,响应快,用户体验更好: React Native比原生简单多了,会Js就可以了,开发 ...
- wifi驱动总结(2)
1.dhd_module_init(驱动模块初始化函数) → dhd_wifi_platform_register_drv(查找设备,注册驱动) → 1) wifi_ctrlfunc_register ...
- Java基础知识二次学习-- 第一章 java基础
基础知识有时候感觉时间长似乎有点生疏,正好这几天有时间有机会,就决定重新做一轮二次学习,挑重避轻 回过头来重新整理基础知识,能收获到之前不少遗漏的,所以这一次就称作查漏补缺吧!废话不多说,开始! 第一 ...
- 开涛spring3(6.9) - 对JDBC的支持 之 7.1 概述
7.1 概述 7.1.1 JDBC回顾 传统应用程序开发中,进行JDBC编程是相当痛苦的,如下所示: //cn.javass.spring.chapter7. TraditionalJdbcTes ...
- kotlin语言使用初体验(一)
居说谷歌新认的干儿子kotlin极为受宠,隐隐有替代Java在 android平台老大位置的趋势.kotlin有谷歌撑腰,加上自己的底子也厚,再之与Java无缝兼容,将来在流行的编程语言中占有一席之地 ...
- 外部无法捕捉Realm的doGetAuthenticationInfo方法抛出的异常
shiro权限框架,用户登录方法的subject.login(token)会进入自定义的UserNamePasswordRealm类的doGetAuthenticationInfo身份验证方法 通常情 ...
- Vulkan Tutorial 05 物理设备与队列簇
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Selecting a physical device 通过VkInstance初始 ...
- nodeJS之事件events
前面的话 events模块是node的核心模块,几乎所有常用的node模块都继承了events模块,比如http.fs等.本文将详细介绍nodeJS中的事件机制 EventEmitter 多数 Nod ...