当前位置:网站首页>MySQL 5.7 takes the first item of the group
MySQL 5.7 takes the first item of the group
2022-07-27 02:50:00 【Crystal heart spring】
Here is an example , Need to take every class 、 The discipline selected the most times , such as 1 There are three students in class who choose math , One who chooses Chinese , So what you expect to return is Mathematics , Others in the same way .
| id | name | class | course |
|---|---|---|---|
| 1 | Zhang San | 1 | mathematics |
| 2 | Li Si | 1 | mathematics |
| 3 | Wang Wu | 1 | mathematics |
| 4 | Zhao Liu | 1 | Chinese language and literature |
| 5 | Black seven | 2 | Chinese language and literature |
| 6 | Pig eight | 2 | Chinese language and literature |
| 7 | Qin Jiu | 2 | mathematics |
| 8 | Wang Shi | 3 | mathematics |
| 9 | Mao Xi | 3 | mathematics |
| 10 | Xiao twelve | 3 | mathematics |
| 11 | A thirteen | 3 | Chinese language and literature |
The first method : limit + group by
example
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 // Sort here
LIMIT 1000 // It's necessary here , And it seems that the maximum value is 1 ten thousand
) t
GROUP BY t.class // The outer layer uses grouping
The results are as follows
| class | course | counts |
|---|---|---|
| 1 | mathematics | 3 |
| 2 | Chinese language and literature | 2 |
| 3 | mathematics | 3 |
I found several blog posts in the station , It's basically this routine , Maybe this method will perform better , And it seems that you can directly get the whole row of data with the maximum value , It's very convenient .
For example, this blog post also analyzes optimization :https://blog.csdn.net/u013066244/article/details/116461584
however : Because the company server is enabled ONLY_FULL_GROUP_BY, So I can't use it .
The second method :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;
The results are as follows
| class | course |
|---|---|
| 1 | mathematics |
| 2 | Chinese language and literature |
| 3 | mathematics |
About this method , It has been written by bloggers , Personally, I think it's very detailed , Connect here :https://blog.csdn.net/MyfishCake/article/details/120175776
边栏推荐
- Talk about the metrics of automated testing
- GoatGui邀你参加机器学习研讨班
- [draw rectangular coordinate system in C language]
- 祝大家七夕快乐,邀你源码共读
- JMeter interface test, quickly complete a single interface request
- 膜拜,阿里内部都在强推的321页互联网创业核心技术pdf,真的跪了
- 平成千字文(へいせいせんじもん) (平成12年9月10日 石渡 明 作) 宇宙広遠 銀河永久 日月運行 不乱無休 地球公転 季節変移 黄道星座 太陽年周 故郷群島 南熱北冷 海洋温暖 気候順良 青空飛雲 諸野深緑 湖泉静息 谷川清流 春桜一面 新芽
- OSPF总结(思维导图)
- Go language slow start -- go operator
- Hcip day 6 OSPF static experiment
猜你喜欢

小姐姐笔记:我是如何学习简单源码拓展视野的

After ten years of testing, I want to say to my friends who are still confused: one thing is to do a good job in personal planning
![[use SQLite3 library to realize student information management system in Visual Studio 2019]](/img/49/5d6c47654e446ab96099a821a16369.png)
[use SQLite3 library to realize student information management system in Visual Studio 2019]

uni-app 微信小程序搜索关键字标红显示

C语言程序的编译(预处理)下
![[redis] five common data types](/img/0f/17945b6f3a7735d9a569296c2c0e2a.png)
[redis] five common data types

NAT network address translation protocol topology experiment

【Redis】五种常用的数据类型

OSPF总结(思维导图)

Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
随机推荐
go语言慢速入门——go运算符
面试突击68:为什么 TCP 需要 3 次握手?
Heisei thousand words (Heisei, September 10, 2012) (Shidu Mingzuo) the universe is far away, the Milky way is permanent, the sun and moon are running disorderly, the earth is open, the seasons shift,
快速排序(Quick sort)
使用注解方式实现 Redis 分布式锁
Applet utils
TCP three handshakes and four disconnects
银河证券基金低佣金开户靠谱吗,可靠安全吗
Function stack frame explanation
多线程的具体使用
膜拜,阿里内部都在强推的321页互联网创业核心技术pdf,真的跪了
go语言慢速入门——包
想要彻底搞的性能优化,得先从底层逻辑开始了解~
动态设置小程序swiper的高度
Greenplum【部署 08】数据库小版本升级流程及问题处理 Error: open-source-greenplum-db-6 conflicts with
小姐姐笔记:我是如何学习简单源码拓展视野的
C语言程序的编译(预处理)下
Multipoint bidirectional republication and routing strategy topology experiment
砺夏行动|源启数字化:既有模式,还是开源创新?
LeetCode->二分查找打卡