Home > Database > Mysql Tutorial > Using MySQL LIMIT, OFFSET for paging?

Using MySQL LIMIT, OFFSET for paging?

WBOY
Release: 2023-08-27 11:25:02
forward
666 people have browsed it

使用 MySQL LIMIT、OFFSET 进行分页?

First we need to create a table with some records and then we will use pagination with the help of limits and offsets.

Create a table with the help of CREATE command. The query is as follows -

mysql> CREATE table limitoffsetDemo
-> (
-> id int,
-> FisrtName varchar(200)
-> );
Query OK, 0 rows affected (0.45 sec)
Copy after login

After creating the table, we will insert records with the help of the INSERT command.

Insert record

mysql> INSERT into limitoffsetDemo values(1,'John');
Query OK, 1 row affected (0.11 sec)

mysql> INSERT into limitoffsetDemo values(2,'Bob');
Query OK, 1 row affected (0.16 sec)

mysql> INSERT into limitoffsetDemo values(3,'David');
Query OK, 1 row affected (0.13 sec)

mysql> INSERT into limitoffsetDemo values(4,'carol');
Query OK, 1 row affected (0.19 sec)

mysql> INSERT into limitoffsetDemo values(5,'Taylor');
Query OK, 1 row affected (0.10 sec)

mysql> INSERT into limitoffsetDemo values(1,'John');
Query OK, 1 row affected (0.24 sec)

mysql> INSERT into limitoffsetDemo values(7,'Smith');
Query OK, 1 row affected (0.13 sec)

mysql> INSERT into limitoffsetDemo values(8,'John');
Query OK, 1 row affected (0.12 sec)

mysql> INSERT into limitoffsetDemo values(9,'Johnson');
Query OK, 1 row affected (0.11 sec)

mysql> INSERT into limitoffsetDemo values(10,'Jason');
Query OK, 1 row affected (0.08 sec)

mysql> INSERT into limitoffsetDemo values(11,'Bk');
Query OK, 1 row affected (0.17 sec)
Copy after login

After inserting the record, we can check how many records there are in my table with the help of the following command Select the command. The query is as follows -

mysql> SELECT * from limitoffsetDemo;
Copy after login

After executing the above query, we will get the following output

+------+-----------+
| id   | FisrtName |
+------+-----------+
| 1    | John      |
| 2    | Bob       |
| 3    | David     |
| 4    | carol     |
| 5    | Taylor    |
| 1    | John      |
| 7    | Smith     |
| 8    | John      |
| 9    | Johnson   |
| 10   | Jason     |
| 11   | Bk        |
+------+-----------+
11 rows in set (0.00 sec)
Copy after login

Now, I have 11 records. To set pagination with the help of limits and offsets, apply the following command Syntax -

SELECT * from yourTableName limit value1,value2;
Copy after login

In the above query, value1 tells us where we need to display the records, and value2 tells us the number of records we need to display. Record.

Now, I apply the above query to get the results as follows -

mysql> SELECT * from limitoffsetDemo limit 7,4;
Copy after login

The following is the output

+------+-----------+
| id   | FirstName |
+------+-----------+
| 8    | John      |
| 9    | Johnson   |
| 10   | Jason     |
| 11   | Bk        |
+------+-----------+
4 rows in set (0.00 sec)
Copy after login

The above is the detailed content of Using MySQL LIMIT, OFFSET for paging?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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