Home > Database > Mysql Tutorial > Retrieve large selection by chunks in MySQL?

Retrieve large selection by chunks in MySQL?

王林
Release: 2023-09-02 22:21:10
forward
813 people have browsed it

在 MySQL 中按块检索大的选择?

To retrieve a large selection in chunks, you need to use ORDER BY LIMIT. The syntax is as follows:

SELECT *FROM yourTableName ORDER BY yourColumnName LIMIT 0,10;
Copy after login

From the above syntax, you will get 10 rows from the table. In the above syntax, 0 represents the first row in the result set of the table, which means it is zero-based indexed. The second value of LIMIT represents the maximum number of rows that can be retrieved from the table.

If you want to get the rows after 10 to 30, then use the following syntax in LIMIT:

SELECT *FROM yourTableName ORDER BY yourColumnName LIMIT 10,20; //11 to 30
Copy after login

If you want another set of rows from 30 to 50, use LIMIT again :

SELECT *FROM yourTableName ORDER BY yourColumnName LIMIT 30 ,20; 31 to 50.
Copy after login

For this you need to use a temporary table. The syntax is as follows:

DROP TEMPORARY TABLE IF EXISTS yourTemporaryTableName;
CREATE TEMPORARY TABLE yourTempTableName
AS
(
   SELECT *FROM yourOriginalTableName
   ORDER BY
   LIMIT 0,100
);
Copy after login

If all records are in the temporary table, then use LIMIT as discussed above to get all records from the temporary table.

SELECT *FROM yourTemporaryTableName LIMIT 0,100;
SELECT *FROM yourTemporaryTableName LIMIT 100,1000;
Copy after login

Now it's up to you to set the limit value. It is a good practice to delete temporary tables now. The query is as follows:

DROP TEMPORARY TABLE yourTemporaryTableName;
Copy after login

Let us demonstrate the above discussion. First create a table. The query to create the table is as follows:

mysql> create table getRecordsDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (1.68 sec)
Copy after login

Use the insert command to insert some records into the table. The query is as follows:

mysql> insert into getRecordsDemo values(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),();
Query OK, 738 rows affected (0.34 sec)
Records: 738 Duplicates: 0 Warnings: 0
Copy after login

Now create a temporary table like the above table. The query to create the temporary table is as follows:

mysql> drop temporary table if exists TempRecord;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create temporary table TempRecord
   -> as
   -> (
   -> select * from getRecordsDemo order by Id limit 0,738
   -> );
Query OK, 738 rows affected (0.03 sec)
Records: 738 Duplicates: 0 Warnings: 0
Copy after login

Now you can use the LIMIT clause to get the results in chunks.

Case 1: The following query is used to get some records from the temporary table 'TempRecord':

mysql> select *from TempRecord limit 0,10;
Copy after login

The following is the output:

+----+
| Id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
+----+
10 rows in set (0.00 sec)
Copy after login

Case 2: Query as follows to obtain the next set of records:

mysql> select *from TempRecord limit 10,20;
+----+
| Id |
+----+
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
| 27 |
| 28 |
| 29 |
| 30 |
+----+
20 rows in set (0.00 sec)
Copy after login

Case 3: Query as follows to obtain another set of records:

mysql> select *from TempRecord limit 30,20;
+----+
| Id |
+----+
| 31 |
| 32 |
| 33 |
| 34 |
| 35 |
| 36 |
| 37 |
| 38 |
| 39 |
| 40 |
| 41 |
| 42 |
| 43 |
| 44 |
| 45 |
| 46 |
| 47 |
| 48 |
| 49 |
| 50 |
+----+
20 rows in set (0.00 sec)
Copy after login

The above is the detailed content of Retrieve large selection by chunks in MySQL?. 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