mysql group by index,mysql GROUP_CONCAT+ GROUP BY + substring_index获取分组的前几名

mysql方法来源于:http://www.cnblogs.com/jjcc/p/5896588.html

###在网上看到一篇,非常赞的方法

比如说要获取班级的前3名,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

得到

20180110214605910576.png

注意 是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

最终结果

20180110214605912529.png

如果单纯使用mysql来写,太复杂,建议使用R :1,用order对score降序排列 2,df[,head(.SD,3), by=class] #对班级分组,返回每组的前三行

原文:http://www.cnblogs.com/nxld/p/6067970.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值