当前位置:网站首页>Mysql 5.7 取分组第一条
Mysql 5.7 取分组第一条
2022-07-26 23:23:00 【水晶心泉】
以下表为例,需要取每个班级、被选的次数最多的学科,比如1班的有三个选数学的,一个选语文的,那么期望返回的是数学,其他同理。
| id | name | class | course |
|---|---|---|---|
| 1 | 张三 | 1 | 数学 |
| 2 | 李四 | 1 | 数学 |
| 3 | 王五 | 1 | 数学 |
| 4 | 赵六 | 1 | 语文 |
| 5 | 黑七 | 2 | 语文 |
| 6 | 猪八 | 2 | 语文 |
| 7 | 琴九 | 2 | 数学 |
| 8 | 王十 | 3 | 数学 |
| 9 | 毛十一 | 3 | 数学 |
| 10 | 萧十二 | 3 | 数学 |
| 11 | 阿十三 | 3 | 语文 |
第一种方法: limit + group by
例
SELECT t.*
FROM
(
SELECT ta.class,ta.course,count(*) as counts
FROM courses as ta
group by ta.class,ta.course
order by counts desc // 在这里排序
LIMIT 1000 //这里是必须的,而且好像最大值是1万
) t
GROUP BY t.class //外层使用分组
返回结果如下
| class | course | counts |
|---|---|---|
| 1 | 数学 | 3 |
| 2 | 语文 | 2 |
| 3 | 数学 | 3 |
在站内找了几篇博文,基本都是这个套路,也许这种方法性能会好一些,而且好像可以直接取到最大值那一整行的数据,可以说非常方便了。
比如这篇博文还对优化进行了分析:https://blog.csdn.net/u013066244/article/details/116461584
但是:由于公司服务器启用了ONLY_FULL_GROUP_BY,所以没法使用。
第二种方法:group_concat+substring_index
select tb.class,SUBSTRING_INDEX(GROUP_CONCAT(tb.course order by counts desc),',',1) as course
from
(
SELECT ta.class,ta.course,count(*) as counts
FROM courses as ta
group by ta.class,ta.course
) as tb
group by tb.class;
返回结果如下
| class | course |
|---|---|
| 1 | 数学 |
| 2 | 语文 |
| 3 | 数学 |
关于这个方法,已经有博主写过了,个人觉得很详细了,连接在此:https://blog.csdn.net/MyfishCake/article/details/120175776
边栏推荐
- 东北证券股票网上开户,手机上开户安全吗
- Multipoint bidirectional republication and routing strategy topology experiment
- [brother Yang takes you to play with the linear table (III) - two way linked list]
- 【洋哥带你玩转线性表(一)——顺序表】
- JMeter下载安装
- TCP的三次握手、四次断开
- 【Code】剑指offer 04二维数组中的查找
- Array methods and loops in JS
- 毕业进入HW,从测试工程师到项目经理,现如今在鹅厂年收入百万,我的给大家的一些建议...
- Hcip bidirectional republication and routing strategy
猜你喜欢

Area optimization of digital chips: detailed explanation of question 1 in the digital direction of the third "Huawei Cup" graduate innovation core competition

JMeter下载安装

【斐波那契数列及螺线 基于C语言】

Redis五种基本数据结构

Record the nth SQL exception

【Code】剑指offer 04二维数组中的查找

After working in Tencent testing post for 5 years, I was ruthlessly dismissed in July, trying to wake up my brother who was still paddling
![[Fibonacci sequence and spiral are based on C language]](/img/11/0dd7ee9a788c519fa3ae5a3f2b0bca.jpg)
[Fibonacci sequence and spiral are based on C language]

Full company mGRE and star topology mGRE
![[draw rectangular coordinate system in C language]](/img/85/cf0c8a8da647888acce5ce3ee871b2.png)
[draw rectangular coordinate system in C language]
随机推荐
C语言 学生信息管理系统 基于数组 可以存取到文本文件
It has been established for 3 years, and now goose factory has an annual income of millions +. As some suggestions of software testing predecessors
【力扣】1859.将句子排序
PPOCRLabel格式的数据集操作总结。
进程的调度
RIP路由信息协议-拓扑实验
Guangguangzai's CSDN journey
HCIP-第三天-广域网拓扑实验
Hcip OSPF comprehensive experiment
HCIP-第五天-OSPF扩展配置实验
Is it useful to lie down with your eyes closed when you can't sleep?
东北证券股票网上开户,手机上开户安全吗
What is the principle of synchronized lock escalation in multithreading?
LeetCode->二分法(三)
NAT网络地址转换协议-拓扑实验
After working in Tencent testing post for 5 years, I was ruthlessly dismissed in July, trying to wake up my brother who was still paddling
想要彻底搞的性能优化,得先从底层逻辑开始了解~
从单表到分表实现数据平滑迁移
Talk about the metrics of automated testing
【你了解Cache吗——全面理解高速缓冲存储器】