English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Is there no double query in MySQL pagination?

To use MySQL pagination, let's first see how to use the CREATE command and create a table with it.

mysql>CREATE table RowCountDemo
   -> (
   -> ID int,
   -> Name varchar(100)
   -> );

Records are inserted with the help of the INSERT command.

mysql>INSERT into RowCountDemo values(1, 'Larry');
mysql>INSERT into RowCountDemo values(2, 'John');
mysql>INSERT into RowCountDemo values(3, 'Bela');
mysql>INSERT into RowCountDemo values(4, 'Jack');
mysql>INSERT into RowCountDemo values(5, 'Eric');
mysql>INSERT into RowCountDemo values(6, 'Rami');
mysql>INSERT into RowCountDemo values(7, 'Sam');
mysql>INSERT into RowCountDemo values(8, 'Maike');
mysql>INSERT into RowCountDemo values(9, 'Rocio');
mysql>INSERT into RowCountDemo values(10, 'Gavin');

Display all records with the help of the SELECT statement.

mysql>SELECT* from RowCountDemo;

The following is the output.

+------+-------+
| ID | Name |
+------+-------+
|    1 | Larry |
|    2 | John |
|    3 | Bela |
|    4 | Jack |
|    5 | Eric |
|    6 | Rami |
|    7 | Sam |
|    8 | Maike |
|    9 | Rocio |
|   10 | Gavin |
+------+-------+
10 rows in set (0.00 sec)

Now let's see the pagination syntax without duplicate queries.

SELECT column_name FROM `yourTableName` WHERE someCondition LIMIT value1, value2;

Now apply the above syntax.

mysql> SELECT ID, Name FROM `RowCountDemo` WHERE ID > 0 LIMIT 0, 11;

This is the output of the above query.

+------+-------+
| ID | Name |
+------+-------+
|    1 | Larry |
|    2 | John |
|    3 | Bela |
|    4 | Jack |
|    5 | Eric |
|    6 | Rami |
|    7 | Sam |
|    8 | Maike |
|    9 | Rocio |
|   10 | Gavin |
+------+-------+
10 rows in set (0.00 sec)