比如说要获取班级的前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获取分组的前几名的更多相关文章

  1. mysql GROUP_CONCAT+ GROUP BY + substring_index获取分组的前几名

    mysql方法来源于:http://www.cnblogs.com/jjcc/p/5896588.html ###在网上看到一篇,非常赞的方法### 比如说要获取班级的前3名,mysql就可以用GRO ...

  2. mysql 查询每个分组的前几名

    按分组排序,并查出每个分组的前3名 单表 SELECT * FROM ( SELECT ZONEID, uid, NAME, fight, IF ( , ) AS rank, ( @zone := z ...

  3. 如何在MySQL中查询每个分组的前几名【转】

    问题 在工作中常会遇到将数据分组排序的问题,如在考试成绩中,找出每个班级的前五名等. 在orcale等数据库中可以使用partition语句来解决,但在mysql中就比较麻烦了.这次翻译的文章就是专门 ...

  4. 如何在mysql中查询每个分组的前几名

    问题 在工作中常会遇到将数据分组排序的问题,如在考试成绩中,找出每个班级的前五名等.  在orcale等数据库中可以使用partition 语句来解决,但在MySQL中就比较麻烦了.这次翻译的文章就是 ...

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

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

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

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

  7. mysql查询每个部门/班级前几名

    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id . +----+-------+--------+--------------+ | I ...

  8. mysql 查询每个班级成绩前两名

  9. SQL Server 2008 R2——分组取前几名

    =================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载  请通过右 ...

随机推荐

  1. 在Mvc中进行异步请求是出现(没有为该对象定义无参数的构造函数)

    解决办法就是给相应的类添加无参数的构造函数:

  2. C++ 元编程 —— 让编译器帮你写程序

    目录 1 C++ 中的元编程 1.1 什么是元编程 1.2 元编程在 C++ 中的位置 1.3 C++ 元编程的历史 2 元编程的语言支持 2.1 C++ 中的模板类型 2.2 C++ 中的模板参数 ...

  3. poj 1001 分析

    1) n = 0; return 1: 2) n = 1; bool standardizeNumNoDot(string &s){标准化是一定要得} _将‘.’前后的〇全部去除,正常retu ...

  4. WPF+MVVM学习总结 DataGrid简单案例

    一.WPF概要 WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,属于.NET Framework 3.0的一部分.它提供了统一的 ...

  5. 学习笔记:javascript 窗口对象(window)

    1.窗口对象属性 属性 描述 closed 返回窗口是否已被关闭. defaultStatus 设置或返回窗口状态栏中的默认文本. document 对 Document 对象的只读引用.请参阅 Do ...

  6. 在Windows上安装MongoDB

    原文官方文档:https://docs.mongodb.org/v2.6/tutorial/install-mongodb-on-windows/ 基于版本:MongoDB 2.6 概览 通过这个示例 ...

  7. 【持续集成】GIT+jenkins+snoar——GIT

    一.GIT基础 1.1 git简介 linux用C语言编写 2005年诞生 分布式管理系统 速度快.适合大规模.跨地区多人协同开发 1.2 本地管理.集中式.分布式 1.3 git安装 #CentOS ...

  8. 解决window.navigator.geolocation.getCurrentPosition在IOS10系统中无法进行地理定位问题

    昨天接到用户通知说在点击"看场地"时无法获取地理位置信息. 在接到通知时,首先想到的是排查机型问题.由于客户多为IOS用户,所以最先看的是在安卓是有没有此问题的发生,调查结果为安卓 ...

  9. SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession作用域(Scope)和生命周期

    可以说每个MyBatis都是以一个SqlSessionFactory实例为中心的.SqlSessionFactory实例可以通过SqlSessionFactoryBuilder来构建.一是可以通过XM ...

  10. C#总结(三)DataGridView增加全选列

    最近的一个winform的项目中,碰到datagridview控件的第一列添加全选的功能,通常这个功能,有两种实现方式:1. 为控件添加DataGridViewCheckBoxColumn来实现,但是 ...