MySQL query to select rows one batch at a time

MySQLMySQLi Database

For this, you can use the concept of LIMIT and OFFSET. Let us first create a table −

mysql> create table DemoTable1514
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> FirstName varchar(20)
   -> );
Query OK, 0 rows affected (0.63 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1514(FirstName) values('Chris');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1514(FirstName) values('Bob');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1514(FirstName) values('Sam');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable1514(FirstName) values('Mike');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1514(FirstName) values('Carol');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1514(FirstName) values('David');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1514(FirstName) values('Robert');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1514(FirstName) values('Adam');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1514(FirstName) values('John');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1514(FirstName) values('Jace');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1514;

This will produce the following output −

+----+-----------+
| Id | FirstName |
+----+-----------+
|  1 | Chris     |
|  2 | Bob       |
|  3 | Sam       |
|  4 | Mike      |
|  5 | Carol     |
|  6 | David     |
|  7 | Robert    |
|  8 | Adam      |
|  9 | John      |
| 10 | Jace      |
+----+-----------+
10 rows in set (0.00 sec)

Here is the query to select rows one batch at a time −

mysql> select * from DemoTable1514 limit 4 offset 4;

This will produce the following output −

+----+-----------+
| Id | FirstName |
+----+-----------+
|  5 | Carol     |
|  6 | David     |
|  7 | Robert    |
|  8 | Adam      |
+----+-----------+
4 rows in set (0.00 sec)
raja
Published on 11-Dec-2019 06:15:23
Advertisements