当前位置:网站首页>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)]
- Three handshakes and four disconnects of TCP
- Multipoint bidirectional republication and routing strategy topology experiment
- MySQL master-slave database configuration based on docker for Ubuntu
- 蚂蚁京东新浪10位架构师424页佳作深入分布式缓存从原理到实践pdf
- #博客大赛# 斗胆尝试浅入门之BAC
- JS 数组去重(含简单数组去重、对象数组去重)
- 线程和进程
- Interrupt, signal, system call
- LeetCode->二分法(三)
猜你喜欢

Hcip day 4 OSPF routing protocol

测试人需要的数据库知识:MySQL常用语法

Rip routing information protocol topology experiment

Witness that the "decoding 2022 strong star of China's network security" is about to set sail

系统安全测试要怎么做,详细来说说

pyqt5使用pyqtgraph画动态散点图

Make static routing accessible to the whole network through ENSP

在腾讯测试岗干了5年,7月无情被辞,想给还在划水的兄弟提个醒.....

JMeter下载安装

砺夏行动|源启数字化:既有模式,还是开源创新?
随机推荐
I wish you a happy Chinese Valentine's day and invite you to read the source code together
Hcip day 6 OSPF static experiment
static关键字
系统安全测试要怎么做,详细来说说
White box test case design (my grandfather can understand it)
Talk about the metrics of automated testing
C语言程序的编译(预处理)下
C language program compilation
快速排序(Quick sort)
想要彻底搞的性能优化,得先从底层逻辑开始了解~
从单表到分表实现数据平滑迁移
无效的目标发行版:17 的解决办法
f8抓交通、f9抓兔子、f10turtle
C language: deep learning recursion
uni-app 微信小程序搜索关键字标红显示
数据库读写分离和分库分表
The latest multi-threaded & highly concurrent learning materials, interview confidence
【Redis】快速入门
Is it necessary to open an account on site for securities speculation? Is it safe to open an account online
平成千字文(へいせいせんじもん) (平成12年9月10日 石渡 明 作) 宇宙広遠 銀河永久 日月運行 不乱無休 地球公転 季節変移 黄道星座 太陽年周 故郷群島 南熱北冷 海洋温暖 気候順良 青空飛雲 諸野深緑 湖泉静息 谷川清流 春桜一面 新芽