Home > Common Problem > Can mysql query the last 10 records?

Can mysql query the last 10 records?

zbt
Release: 2023-07-25 17:12:22
Original
3218 people have browsed it

mysql can query the last 10 records. The method is: 1. Create a table and insert test data; 2. Execute the query; 3. The query will output the last 10 records, sorted in descending order by ID.

Can mysql query the last 10 records?

The operating environment of this tutorial: windows10 system, mysql8.0.16 version, DELL G3 computer.

MySQL is a popular relational database management system that is often used to store and retrieve large amounts of data. When processing data, sometimes we need to get only the last few records, which can be very useful in some cases. This article will introduce how to use MySQL query to obtain the last 10 records.

1. Use the LIMIT clause to query the last 10 records

In MySQL, we can use the LIMIT clause to limit the number of query results. To query the last 10 records, we can pass the LIMIT clause with ORDER BY clause is used in combination to achieve this. The following is an example of querying the last 10 records:

SELECT * FROM table_name ORDER BY primary_key_column DESC LIMIT 10;

In the above query, table_name is the name of the table to be queried, and primary_key_column is the primary key of this table. Column name. We use the DESC keyword to sort the query results in descending order of the primary key column, and then use LIMIT 10 to limit the number of results to the last 10. Table and column names can be adjusted as needed.

2. Example Demonstration

Suppose we have a table named students, which contains information such as students’ names, ages, grades, etc. The primary key is id. We will use the above method to query the records of the last 10 students.

Create students table and insert test data:

CREATE TABLE students (

id INT PRIMARY KEY AUTO_INCREMENT,

name VARCHAR(50),

age INT,

score FLOAT

);

INSERT INTO students (name, age, score) VALUES

('Tom' , 18, 85.5),

('Kate', 19, 92.0),

('John', 20, 78.2),

('Amy', 18 , 90.5),

('Mike', 19, 88.3),

('Lisa', 20, 75.0),

('Sam', 18, 82.5 ),

('Alice', 19, 87.5),

('David', 20, 83.7),

('Lucy', 18, 89.0),

('Peter', 19, 91.2),

('Eva', 20, 86.8);

Execute query:

SELECT * FROM students ORDER BY id DESC LIMIT 10;

This query will output the last 10 student records, arranged in descending order by id:

---- ------- --- -- -------

| id | name | age | score |

---- ------- ----- ---- ---

| 12 | Eva | 20 | 86.8 |

| 11 | Peter | 19 | 91.2 |

| 10 | Lucy | 18 | 89.0 |

| 9 | David | 20 | 83.7 |

| 8 | Alice | 19 | 87.5 |

| 7 | Sam | 18 | 82.5 |

| 6 | Lisa | 20 | 75.0 |

| 5 | Mike | 19 | 88.3 |

| 4 | Amy | 18 | 90.5 |

| 3 | John | 20 | 78.2 |

---- ------- ----- -------

Conclusion:

By using MySQL’s LIMIT and ORDER BY clause, we can easily query and get the last 10 records. This method is very convenient for database tables that need to be sorted according to the primary key. Whether it is student information or other types of data, the final subset can be obtained through the above method .

The above is the detailed content of Can mysql query the last 10 records?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template