Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of querying the maximum value by mysql

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what the maximum value of mysql query methods are, the editor thinks it is very practical, so share with you to do a reference, I hope you can get something after reading this article.

The maximum value statement of the mysql query is: first, by grouping the stuname field, and then using the MAX function to calculate the maximum value in each group, the code is [SELECT an a.score Max (a.score) maximum].

The maximum mysql query statement is:

1. Method 1

SELECT a.accouname.MAX (a.score) AS score FROM stuscore a GROUP BY a.`roomame`

In this statement, we group the stuname field, and then use the MAX () function to calculate the maximum value in each group.

2. Method 2: use connection

SELECT a.scoamere` a.score AS score FROM stuscore a JOIN stuscore b ON a.`scoame` = b.`score`GROUP BY a.`score`HAVING a.`score` = MAX (b.`score`)

In the second sql statement, we join the two tables with stuname as the judgment condition. If you only execute

SELECT a.stuname,a.score AS score FROM stuscore a JOIN

Stuscore b ON a.stuname=b.stuname, we get the following result set:

This result set is obtained because when joining, each record in the left table is matched according to the join condition ON a.stuname=b.stuname "and each record in the right table, which is why duplicate records occur. This is a bit like a" double loop "in a programming language.

Then add the grouping and grouping condition GROUP BY a.score HAVING a.score=MAX (b.score), and you can get the maximum score of each student.

In fact, I didn't quite understand the grouping condition GROUP BY a.`score `score` = MAX (b.`score`) at first, that is, I grouped according to the score field, but why the MAX () function here can calculate the maximum score of each student? we know that the first method is to group each student's name, and then use the MAX () function to calculate the maximum value, which is for sure. But the second method is to group the score field. I think the reason here is that when joining the two tables, the join condition a.stuname=b.stuname has already grouped the records according to the student name, so MAX (b.score) can calculate the maximum score for each student.

On the maximum value of mysql query methods which are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report