Detailed example of getting the maximum value of each group after grouping in MySQL

Detailed example of getting the maximum value of each group after grouping in MySQL

Detailed example of getting the maximum value of each group after grouping in MySQL

1. The test database table is as follows:

create table test 
( 
  `id` int not null auto_increment, 
  `name` varchar(20) not null default '', 
  `score` int not null default 0, 
  primary key (`id`) 
)engine=InnoDB CHARSET=UTF8; 

2. Insert the following data:

mysql> select * from test; 
+----+----------+-------+ 
| id | name | score | 
+----+----------+-------+ 
| 1 | jason | 1 | 
| 2 | jason | 2 | 
| 3 | jason | 3 | 
| 4 | linjie | 1 | 
| 5 | linjie | 2 | 
| 6 | linjie | 3 | 
| 7 | xiaodeng | 1 | 
| 8 | xiaodeng | 2 | 
| 9 | xiaodeng | 3 | 
| 10 | hust | 2 | 
| 11 | hust | 3 | 
| 12 | hust | 1 | 
| 13 | haha ​​| 1 | 
| 14 | haha ​​| 2 | 
| 15 | dengzi | 3 | 
| 16 | dengzi | 4 | 
| 17 | dengzi | 5 | 
| 18 | shazi | 3 | 
| 19 | shazi | 4 | 
| 20 | shazi | 2 | 
+----+----------+-------+ 

3. The following is the key point. The purpose is to group by name , and then after grouping, get the highest score in each group. The sql is as follows

select a.* from test a inner join (select name,max(score) score from test group by name)b on a.
name=b.name and a.score=b.score order by a.name; 

Of course, the last order by a.name above can be removed

4. The test results are as follows:

+----+----------+-------+ 
| id | name | score | 
+----+----------+-------+ 
| 3 | jason | 3 | 
| 6 | linjie | 3 | 
| 9 | xiaodeng | 3 | 
| 11 | hust | 3 | 
| 14 | haha ​​| 2 | 
| 17 | dengzi | 5 | 
| 19 | shazi | 4 | 
+----+----------+-------+ 

5. Many methods on the Internet are wrong, such as the following, which will not work if tested personally

select * from (select * from test order by score desc) t group by name order by score desc limit 4; 
select score,max(score) from test group by name; 
select * from test where score in (select max(score) from test group by name); 
select * from test where score in (select substring_index(group_concat(score order by score desc separator ','),',',1) from test group by name); 
 
select * from (select name,score,ROW_NUMBER() over(group by name order by score desc) as rowNum from test) rank where rank.rowNum <=1 order by rank.score desc; 
 
select * from( select StoresNo,[CustomerCaseNo],[PaymentsTime], ROW_NUMBER() over(partition by CustomerCaseNo order by [PaymentsTime] desc) as rowNum 
from BAL_paymentsSwiftInfo where StoresNo='zq00000034') ranked where ranked.rowNum <= 1 order by ranked.CustomerCaseNo, ranked.PaymentsTime desc 
 
select * from (select * from test order by score desc) as a group by a.name; 

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • MySql grouping and randomly getting one piece of data from each group
  • Implement group by based on MySQL to get the latest data of each group
  • MySQL subqueries and grouped queries
  • MySQL grouping queries and aggregate functions
  • MySql Group By implements grouping of multiple fields
  • Detailed explanation of MySQL group sorting to find the top N
  • Mysql uses group by group sorting
  • Detailed explanation of MySQL data grouping

<<:  Methods and steps for Etcd distributed deployment based on Docker

>>:  Sample code for configuring nginx to support https

Recommend

How to use Vue-router routing

Table of contents 1. Description 2. Installation ...

JavaScript canvas text clock

This article example shares the specific code of ...

A brief discussion on whether CSS will block page rendering

Maybe everyone knows that js execution will block...

IE8 browser will be fully compatible with Web page standards

<br />According to foreign media reports, in...

CSS multi-level menu implementation code

This is a pretty cool feature that makes web page...

Tomcat server security settings method

Tomcat is an HTTP server that is the official ref...

Detailed explanation of the functions of each port of Tomcat

From the tomcat configuration file, we can see th...

JavaScript to achieve JD.com flash sale effect

This article shares the specific code of JavaScri...

The implementation process of extracting oracle data to mysql database

In the migration of Oracle database to MySQL data...

Writing methods that should be prohibited in native JS

Table of contents Block-level functions Directly ...

Some understanding of absolute and relative positioning of page elements

From today on, I will regularly organize some smal...

How to import SQL files in Navicat Premium

I started working on my final project today, but ...