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——分组取前几名
=================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载 请通过右 ...
随机推荐
- IOS的UIPickerView 和UIDatePicker
1.UIPickerView的常见属性 //数据源(用来告诉UIPickerView有多少列多少行) @property(nonatomic,assign) id<UIPikerViewData ...
- IOS中的UIScrollView
要引用UIScrollView 首先要遵循UIScrollViewDelegate协议 然后重写 //1.拖拽方法 -(void)scrollViewDidScroll:(UIScrollView * ...
- Gartner:当商业智能成熟度低时,如何加快分析采用率
文 | 水手哥 本文出自:知乎专栏<帆软数据应用研究院>--数据干货&资讯集中地 根据Gartner近7年的调查结果,71%的受访企业处于低成熟度阶段,也就是Gartner五级BI ...
- 工具类总结---(三)---MD5加密
用于给文件名等进行MD5加密: import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; / ...
- Apache localhost和局域网ip地址访问
今天忍无可忍重装了公司的电脑,所以把开发工具也都重新装一下. 安装wamp,localhost和局域网ip地址无法访问. 在C:\Windows\System32\drivers\etc\hosts文 ...
- POJ1850-Code 递推数学
题目链接:http://poj.org/problem?id=1850 题目大意: 按照字典序对升序排列组成的字母进行编号,给出一个长度不超过10的串,求出它的编号是多少?如果无法进行编号则输出0. ...
- 精益IT组织与分享式领导
精益IT组织 未来的组织将专注于同行业的产品或业务流--其他的一切,包括专家和管理者在内,都是为了让一线工作人员可以第一时间就做好,而又不会遇到任何麻烦.最大的制约不是技术:真正的挑战是变 ...
- Ajax,纯Js+Jquery
AJAX:Asynchronous Javascript and xml 异步,Js和Xml 交互式网页开发 不刷新页面,与服务器交互 详情请参照Jquery工具指南用在浏览器端的技术,无刷新,通过X ...
- VR全景加盟-全景智慧城市携万千创业者决战BAT
在所谓互联网思维走到末路.可穿戴设备基本昙花一现的大环境下,很多互联网人员转战VR市场,自然喜欢用互联网思维来考虑.笔者认识一些投资界人士,在谈到投资时,他们经常就问以下几句话:2B还是2C?将来有多 ...
- python 列表、元组、字符串、字典、集合、return等梳理
有必要对这些数据类型及操作做下梳理: 1.列表:增删改查 a.查找: >>> names=["zhang","wang","li&q ...