Home > Database > Mysql Tutorial > body text

How to query the first 10 records in mysql

青灯夜游
Release: 2021-12-02 16:02:14
Original
32777 people have browsed it

In mysql, you can use the SELECT query statement and the limit keyword to query the first 10 records. The syntax is "SELECT * FROM data table LIMIT 10;" or "SELECT * FROM data table LIMIT 0,10; ".

How to query the first 10 records in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

In mysql, you can use the SELECT statement and the limit keyword to query the first 10 records.

In MySQL, you can use the SELECT statement to query data. Querying data refers to using different query methods to obtain different data from the database according to needs. It is the most frequently used and important operation.

LIMIT is used to specify which record the query results start to be displayed and how many records are displayed in total.

Grammar:

SELECT
{* | <字段列名>}
FROM <表 1>, <表 2>…
[LIMIT 子句]
Copy after login

Syntax of LIMIT clause:

1. Do not specify the initial position

LIMIT 记录数
Copy after login
# When the ##LIMIT keyword does not specify an initial position, records will be displayed starting from the first record. The number of records displayed is specified by the LIMIT keyword.

"Number of records" indicates the number of records displayed. If the value of "Number of records" is less than the total number of query results, the specified number of records will be displayed starting from the first record. If the value of "Number of records" is greater than the total number of query results, all the queried records will be displayed directly.

Example:


mysql> SELECT * FROM tb_students_info LIMIT 10;
+----+--------+---------+------+------+--------+------------+
| id | name   | dept_id | age  | sex  | height | login_date |
+----+--------+---------+------+------+--------+------------+
|  1 | Dany   |       1 |   25 | F    |    160 | 2015-09-10 |
|  2 | Green  |       3 |   23 | F    |    158 | 2016-10-22 |
|  3 | Henry  |       2 |   23 | M    |    185 | 2015-05-31 |
|  4 | Jane   |       1 |   22 | F    |    162 | 2016-12-20 |
|  5 | Jim    |       1 |   24 | M    |    175 | 2016-01-15 |
|  6 | John   |       2 |   21 | M    |    172 | 2015-11-11 |
|  7 | Lily   |       6 |   22 | F    |    165 | 2016-02-26 |
|  8 | Susan  |       4 |   23 | F    |    170 | 2015-10-01 |
|  9 | Thomas |       3 |   22 | M    |    178 | 2016-06-07 |
| 10 | Tom    |       4 |   23 | M    |    165 | 2016-08-05 |
+----+--------+---------+------+------+--------+------------+
10 rows in set (0.26 sec)
Copy after login

2. Specify the initial position

LIMIT 初始位置,记录数
Copy after login

The LIMIT keyword can specify which record the query results start to be displayed. How many records to display.

  • "Initial position" indicates which record to start displaying;

  • "Number of records" indicates the number of records to display.

The position of the first record is 0, and the position of the second record is 1. The subsequent records are deduced in sequence.

Note: The two parameters after LIMIT must be positive integers.

mysql> SELECT * FROM tb_students_info LIMIT 0,10;
+----+--------+---------+------+------+--------+------------+
| id | name   | dept_id | age  | sex  | height | login_date |
+----+--------+---------+------+------+--------+------------+
|  1 | Dany   |       1 |   25 | F    |    160 | 2015-09-10 |
|  2 | Green  |       3 |   23 | F    |    158 | 2016-10-22 |
|  3 | Henry  |       2 |   23 | M    |    185 | 2015-05-31 |
|  4 | Jane   |       1 |   22 | F    |    162 | 2016-12-20 |
|  5 | Jim    |       1 |   24 | M    |    175 | 2016-01-15 |
|  6 | John   |       2 |   21 | M    |    172 | 2015-11-11 |
|  7 | Lily   |       6 |   22 | F    |    165 | 2016-02-26 |
|  8 | Susan  |       4 |   23 | F    |    170 | 2015-10-01 |
|  9 | Thomas |       3 |   22 | M    |    178 | 2016-06-07 |
| 10 | Tom    |       4 |   23 | M    |    165 | 2016-08-05 |
+----+--------+---------+------+------+--------+------------+
10 rows in set (0.26 sec)
Copy after login
[Related recommendations:

mysql video tutorial]

The above is the detailed content of How to query the first 10 records in mysql. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!