当前位置:网站首页>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

原网站

版权声明
本文为[User 7741497]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011900317056.html