当前位置:网站首页>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
边栏推荐
- [dimension reduction blow, take you to learn CPU in depth (Part 1)]
- MySQL master-slave database configuration based on docker for Ubuntu
- As for the pit saved by serialized variables, the data added with indexer cannot be serialized
- Go language slow start -- go operator
- 进程的调度
- Hcip day 6 OSPF static experiment
- 砺夏行动|源启数字化:既有模式,还是开源创新?
- [Li Kou] 1859. Sort sentences
- Getlocation:fail the API need to be declared in the requiredprivateinfo field in app.json
- 中断、信号、系统调用
猜你喜欢

Hcip OSPF interface network interface type experiment

Hcip the next day

Redis安装及运行(linux)

无效的目标发行版:17 的解决办法

Interview shock 68: why does TCP need three handshakes?

Jmeter接口测试, 快速完成一个单接口请求

毕业进入HW,从测试工程师到项目经理,现如今在鹅厂年收入百万,我的给大家的一些建议...
![[redis] quick start](/img/42/09f08b4f78bc2ddd4d15693d62cb4b.png)
[redis] quick start

Web3.0 world knowledge system sharing - what is Web3.0

从初级程序员到架构师学习路线+配套学习资源完整版
随机推荐
Hcip day 6 OSPF static experiment
C language program compilation
Web3.0世界知识体系分享-什么是Web3.0
Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
Leetcode skimming -- no.238 -- product of arrays other than itself
Mysql 5.7 取分组第一条
Hcip day 4 OSPF routing protocol
time模块: 时间戳、结构化时间、格式化时间的获取与相互转化
C language: deep learning recursion
js utils 零碎
测试人需要的数据库知识:MySQL常用语法
White box test case design (my grandfather can understand it)
Three handshakes and four disconnects of TCP
#博客大赛# 斗胆尝试浅入门之BAC
项目时区问题解决
Go language slow start - package
Prometheus operation and maintenance tool promtool (III) debug function
Hcip day 5 OSPF extended configuration experiment
小姐姐笔记:我是如何学习简单源码拓展视野的
Redis五种基本数据结构