
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to select rows one batch at a time
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)
- Related Articles
- How to multiple insert or batch insert at a time in MySQL query?
- MySQL query to select too many rows?
- MySQL query to select multiple rows effectively?
- MySQL query to select rows older than a week?
- MySQL query to select top n rows efficiently?
- MySQL select query to select rows from a table that are not in another table?
- MySQL query to select records with a particular date and time?
- MySQL query to select rows except first row in descending order?
- How to order results of a query randomly & select random rows in MySQL?
- How to fetch only N rows at a time in MySQL?
- How to select one item at a time from JCheckBox in Java?
- MySQL - How to count all rows per table in one query?
- How to select all rows from a table except the last one in MySQL?
- MySQL query to select one specific row and another random row?\n
- Select all duplicate MySQL rows based on one or two columns?

Advertisements