[MySQL] How to get the entire data in a table with the maximum value and the penultimate one in a certain field in MySQL?

foreword

In MySQL, we often need to manipulate the data in the database. Sometimes we need to get the second last record in the table. This requirement may seem simple, but if you don't know the correct SQL query statement, you may waste a lot of time.

In this article, we will explore how to get the second last record in a table using a MySQL query.

1. Query the penultimate record

There are multiple ways to query the penultimate record in MySQL, below we will introduce the three most widely used methods.

1.1. Use ranking

We can use the ranking method, exclude the last record, and return the record with the second rank. We can use the following query to achieve this:

SELECT * FROM table_name ORDER BY id DESC LIMIT 1,1;

Among them, table_name represents your table name, and id represents an auto-increment ID (or other unique value) in your table. Use the DESC keyword to sort your records in reverse order. LIMIT 1, 1 indicates that we want to skip the last record, and then return only one, which is the second. This method is simpler, but can be slower when dealing with large tables.

1.2. Sub query

Another way to get the second-to-last record is to use a subquery. We first query the last record in the table, and then query the previous record.

SELECT * FROM table_name WHERE id=(SELECT MAX(id)-1 FROM table_name)

This method uses a subquery to get the penultimate record, and the result can be obtained directly.

1.3. Nested query

The third method is to use nested queries, querying the last record and the penultimate record separately, and merging the results together.

SELECT * FROM table_name WHERE id= (
    SELECT id FROM (
        SELECT id FROM table_name ORDER BY id DESC LIMIT 1, 1
    ) tmp
)

This method requires the use of nested queries and multiple subqueries, so it is more complicated. However, using this method reduces network bandwidth usage and therefore performs faster in some cases.

Second, the following provides a test case for you

Let's look at an example, suppose we have a table called users with the following fields:

CREATE TABLE users (
    id INT(11) NOT NULL AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    age INT(11),
    PRIMARY KEY (id)
);

Now insert some records into the table:

INSERT INTO users(name,age) VALUES('Tom',21);
INSERT INTO users(name,age) VALUES('Jerry',22);
INSERT INTO users(name,age) VALUES('Lucy',23);
INSERT INTO users(name,age) VALUES('Lily',24);
INSERT INTO users(name,age) VALUES('Mike',25);

Now we want to query the penultimate record:

SELECT * FROM users ORDER BY id DESC LIMIT 1,1;

This will return Lily's records:

+----+------+-----+
| id | name | age |
+----+------+-----+
|  4 | Lily |  24 |
+----+------+-----+

3. Query the entire piece of data with a certain field as the maximum value

3.1, use max

SELECT name,class,max(score) score from score_test GROUP BY class

3.2, use connection

SELECT a.stuname,a.score AS score FROM stuscore a JOIN 
stuscore b ON a.`stuname`=b.`stuname` 
 GROUP BY a.`score` HAVING a.`score`=MAX(b.`score`);

3.3, the first n maximum (minimum) values

SELECT c.stuname,c.score FROM (SELECT a.stuname,a.score,(SELECT COUNT(*) FROM stuscore b WHERE b.score>a.score AND b.stuname=a.stuname) AS cnt 
FROM stuscore a ) c WHERE c.cnt<=1  GROUP BY c.stuname,c.score;

4. Use combined query to find out what the minimum price is, and then use this price to find out the corresponding data.

4.1. Use combined query to find out what the minimum price is, and then use this price to find out the corresponding data.

SELECT * FROM commodity WHERE price = (SELECT MIN(price) FROM commodity)

4.2. Use ORDER BY to group prices, use ASC to sort them in ascending order, and then use LIMIT to get the first piece of data.

	SELECT * FROM commodity ORDER BY price ASC LIMIT 1;

in conclusion

There are multiple ways to get the second-to-last record in a table in MySQL. This can be easily achieved using one of three rankings, subqueries, and nested queries. Which method to use will depend on your specific needs and the size of your table. In practical applications, the most appropriate method should be selected according to the actual situation to achieve the best performance.

Guess you like

Origin blog.csdn.net/u011397981/article/details/131775747