当前位置:网站首页>MySQL groups first and then takes the largest and smallest records in the group
MySQL groups first and then takes the largest and smallest records in the group
2022-08-07 22:03:00 【wr_01】
需求:对MySQLAll records are grouped,Take the maximum value of a field in the group/最小值
解决办法:
1.Sort first and then group to get the first record after sorting(Applies to duplicate records)
环境准备:mysql5.7
背景:求每个classThe oldest student under the category
数据准备:
| id | name | age | class |
|---|---|---|---|
1 张三 15 1
2 李四 16 1
3 王五 16 2
4 赵六 15 2
5 钱七 14 2
6 孙八 16 2
7 李九 15 1
8 周十 16 1
在Mysql中,通过GroupBy分组后,will take the primary keyIDThe smallest piece of data is used as the grouped data.If there is demand,After sorting according to other fields, take the maximum or minimum value of the corresponding field,通过SQLStatements are achievable,The core is that it needs to be added after sortingLimit xx.如果不加Limit xx,Then the data for each group will be selected by defaultid最小的那条记录.
区别如下:
select * from
(select * from student2 ORDER BY age desc limit 1000) record
GROUP BY record.class;

select * from
(select * from student2 ORDER BY age desc) record
GROUP BY record.class;

思路:
1.先排序
2.Then group and take the first record of sorting
2.通过max(), min()It can realize the function of sorting first and then grouping(Applies to no duplicate records)
背景:求不同typeA record of the most recent time operation of type
数据准备:
id name type create_time
1 记录一 1 2022-07-31 14:17:42
2 记录二 2 2022-07-31 14:18:02
3 记录三 1 2022-07-31 14:18:16
4 记录四 2 2022-07-31 14:18:27
5 记录五 1 2022-07-31 14:18:48
select * from record a right join
(select type as b_type,max(create_time) as max_create_time from record GROUP BY type) b
on a.type = b.b_type and a.create_time = b.max_create_time

思路;
1.先用maxto find out the time max
2.然后用right join 关联主表, Find out the full amount of information
边栏推荐
- 台式电脑亮度怎么调整 台式电脑亮度在哪里调
- 打印机怎么停止打印以前的任务 怎么取消打印机正在打印的文件
- 数据仓库调度工具Azkaban的使用(二)
- golang切片slice详解:介绍、内存分析、定义、遍历、切片可以继续切片、切片可以动态增长、切片的拷贝及底层原理
- Installing mysql under centos7.9 under Linux and some problems encountered
- 新电脑如何分区硬盘 新电脑分区怎么分区
- Leecode-SQL 1407. Top Traveler
- [笔记]攻防工具分享之 CobaltStrike框架 《一》环境配置
- win10系统玩部分老游戏时提示0xc0000022的解决方法.
- LeetCode_双指针_中等_633.平方数之和
猜你喜欢

【kali-权限提升】(4.2.3)社会工程学工具包:二维码组合攻击

Towards a Unified View of Parameter-Efficient Transfer Learning

Win7系统msfeedssync.exe是什么进程如何关闭

golang方法的使用细节:参数默认是值拷贝,不仅仅是struct自定义数据类型也可以绑定方法、方法名称首字母大写为public权限、String()方法的使用

如何打开本地组策略编辑器 打开组策略编辑器方法

Notebook computer memory that can hold laptop memory how to install

Flutter for Web:为什么 Flutter 最适合 Web 应用开发?

Linux下centos7.9版本下安装mysql及遇见的一些问题

Use phpstudy (small skin panel) to configure the record of the local PHP development environment

数据仓库调度工具Azkaban的使用(二)
随机推荐
Dp array to solve knapsack problem
怎么调整显示器刷新率_电脑屏幕怎么调刷新率
怎么查询宽带账号密码 如何查宽带账号和密码
7zip自带hash校验功能
Unity编辑器拓展--预览窗口拓展
如何找到系统支持的最大并发数
怎么打开win7任务管理器的六种最详细方法
那就浅浅回顾一下生成器吧
[笔记]机器学习之机器学习模型及案例分析《一》
Time to learn about mmap
详解中断系统
golang方法的使用细节:参数默认是值拷贝,不仅仅是struct自定义数据类型也可以绑定方法、方法名称首字母大写为public权限、String()方法的使用
一个较重的代码坏味:“炫技式”的单行代码
How to change the default opening method How to change the opening method of a file
Leecode-SQL 1407. Top Traveler
ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法
Leecode-SQL 1393. Capital Gains and Losses on Stocks
2022G1工业锅炉司炉特种作业证考试题库模拟考试平台操作
win10系统玩部分老游戏时提示0xc0000022的解决方法.
无线蓝牙耳机如何连接电脑 无线蓝牙耳机能连电脑吗