当前位置:网站首页>The maximum (minimum, latest and top n) records are taken for MySQL grouping
The maximum (minimum, latest and top n) records are taken for MySQL grouping
2022-06-11 17:50:00 【User 7741497】
In the process of database development , We need to extract the first few records for each type of data , Or take the latest 、 Minimum 、 Max, wait , How to achieve this , This article introduces how to realize mysql Grouping takes the maximum ( Minimum 、 newest 、 front N strip ) Bar record . You can refer to what you need .
Let's take a look at the data to be used in this example
Create tables and insert data :
CREATE TABLE `tb` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(10) CHARACTER SET latin1 DEFAULT NULL, `val` int(11) DEFAULT NULL, `memo` varchar(20) CHARACTER SET latin1 DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
insert into tb values('a', 2, 'a2');
insert into tb values('a', 1, 'a1');
insert into tb values('a', 3, 'a3');
insert into tb values('b', 1, 'b1');
insert into tb values('b', 3, 'b3');
insert into tb values('b', 2, 'b2');
insert into tb values('b', 4, 'b4');
insert into tb values('b', 5, 'b5');The data table is as follows :
name | val | memo |
|---|---|---|
a | 2 | a2 |
a | 1 | a1 |
a | 3 | a3 |
b | 1 | b1 |
b | 3 | b3 |
b | 2 | b2 |
b | 4 | b4 |
b | 5 | b5 |
Press name Take... In groups val The data of the row with the largest value
Method 1 :
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
Method 2 :
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
Method 3 :
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
Method four :
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by
Method five :
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
Methods six :
select * from (select * from tb ORDER BY val desc) temp GROUP BY name ORDER BY val desc;
The results of the above six methods are as follows :
name | val | memo |
|---|---|---|
a | 3 | a3 |
b | 5 | b5 |
Xiaobian recommends to use the first 、 Third 、 The fourth clock method , Result display page 1,3,4 The two methods are equally efficient , The first 2,5 This method is less efficient .
Press name Take... In groups val The data of the row with the smallest value
Method 1 :
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
Method 2 :
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
Method 3 :
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
Method four :
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
Method five :
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
The results of the above five methods are as follows :
name | val | memo |
|---|---|---|
a | 1 | a1 |
b | 1 | b1 |
Press name Group the data of the first row
sql as follows :
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name // This is sql server Of //mysql Should be select a.* from tb a where val = (select val from tb where name = a.name limit 1) order by a.name
give the result as follows :
name | val | memo |
|---|---|---|
a | 2 | a2 |
b | 1 | b1 |
边栏推荐
- Difference between require and ES6 import
- What problems are exposed when all Sohu employees are cheated?
- 04_ Feature engineering feature selection
- Service learning notes 03 front desk service practice
- Authoring share | understanding saml2 protocol
- ffmpeg硬编解码 Inter QSV
- Qlineedit set input mask
- Tidb lightning configuration data restore route
- [online problem] timeout waiting for connection from pool
- Test basis: black box test
猜你喜欢

tidb-写热点的测试及分析

测试基础之:黑盒测试

Use of forcescan in SQL server and precautions

ADB command learning notes

Custom or subscription? What is the future development trend of China's SaaS industry?

【先收藏,早晚用得到】49个Flink高频面试题系列(一)

端口规划与APJ

Bracket generation ---2022/02/25

安装mariadb 10.5.7(tar包安装)

which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mod
随机推荐
ADB command learning notes
6-3 读文章(*)
spawn ./gradlew EACCES at Process.ChildProcess._handle.onexit
How to simplify a lot of if... Elif... Else code?
开源项目那么多,这次带你了解个版本的区别,明白alpha版、beta版、rc版是什么意思
Windows technology - how to view the instruction set, model, attribute and other details supported by the CPU, and how to use the CPU-Z tool to view the processor, memory, graphics card, motherboard,
Mathematical basis of information security Chapter 2 - congruence
【线上问题】Timeout waiting for connection from pool 问题排查
Service learning notes 01 start method and life cycle
Test and analysis of tidb write hotspot
合并两个有序链表---2022/02/24
R language to find missing value location of data set
自动化测试-Selenium
Centos7 server configuration (IV) -- installing redis
Leetcode force deduction question
Mathematical basis of information security Chapter 4 -- quadratic residual and square root
6-8 creating and traversing linked lists
tidb-lightning配置数据还原路由
Go path: goroot & gopath
6-6 批量求和(*)