Home Database Mysql Tutorial How to implement paging query in MySQL

How to implement paging query in MySQL

Mar 16, 2019 pm 02:01 PM
Paging query

Paging query is implemented in MySQL: When the amount of data is small, limit query can be used to implement paging query. When the amount of data is large, it can be implemented by establishing a primary key or unique index. In addition, it can be sorted by order by.

Admin management is always unavoidable in back-end projects. When back-end management needs to display data, it will need to use paging. Next, in this article, I will share with you how to implement paging query in MySQL, with It serves as a reference and I hope it will be helpful to everyone.

How to implement paging query in MySQL

【Recommended course: MySQL Tutorial

Generally, when performing paging queries in MySQL, limit query will be used, and order by will be used in the query for sorting. Next, we will introduce in detail how MySQL implements paging query

Paging requirements:

The client passes start (page number), limit (number of items displayed on each page) ) two parameters to query the data in the database table by paging. MySql database provides paging functions with limit m, n, but the usage of this function is different from our needs, so you need to rewrite the paging statement that suits you according to the actual situation. . Example

The sql for querying data from items 1 to 10 is:

select * from table limit 0,10;
Copy after login

Corresponding to our needs is to query the data on the first page:

select * from table limit (1-1)*10,10;
Copy after login

From the above From the analysis, we can conclude that the format of paging sql is:

 select * from table limit (start-1)*limit,limit;
Copy after login

where start is the page number and limit is the number of items displayed on each page.

Establishing a primary key or unique index

Using limit for data paging when the data volume is small will not cause significant slowdown in performance, but when the data volume reaches When the level reaches 10,000 or 1,000,000, the performance of SQL statements will affect the return of data. This is to use the primary key or unique index instead of limit for data paging

Example: Return data between 10 and 50

Set the primary key or unique index to demo_id

select * from table where demo_id > (pageNo-1)*pageSize limit pageSize;
Copy after login

Reordering based on data

When the information needs to be returned in order or reverse order, the above data needs to be sorted. Order by ASC can be used to express the order, and order by DESC can be used to express the reverse order. Generally, the default is the order.

Example: The returned data is arranged in the order of demo_id

select * from table where demo_id > (pageNo-1)*pageSize order by demo_id limit pageSize;
Copy after login

Summary: The above is this article The entire content, I hope it will be helpful to everyone

The above is the detailed content of How to implement paging query in MySQL. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP database connection to implement paging query How to use PHP database connection to implement paging query Sep 08, 2023 pm 02:28 PM

How to use PHP database connection to implement paging query. When developing web applications, it often involves the need to query the database and perform paging display. As a commonly used server-side scripting language, PHP has powerful database connection functions and can easily implement paging queries. This article will introduce in detail how to use PHP database connection to implement paging query, and attach corresponding code examples. Prepare the database Before we start, we need to prepare a database containing the data to be queried. Here we take the MySQL database as an example,

PHP and PDO: How to perform paging queries and display data PHP and PDO: How to perform paging queries and display data Jul 29, 2023 pm 04:10 PM

PHP and PDO: How to query and display data in pages When developing web applications, querying and displaying data in pages is a very common requirement. Through paging, we can display a certain amount of data at a time, improving page loading speed and user experience. In PHP, the functions of paging query and display of data can be easily realized using the PHP Data Object (PDO) library. This article will introduce how to use PDO in PHP to query and display data by page, and provide corresponding code examples. 1. Create database and data tables

How to create high-performance MySQL data paging queries using Go language How to create high-performance MySQL data paging queries using Go language Jun 17, 2023 am 09:09 AM

With the rapid development of the Internet, data processing has become an important skill in enterprise application development. MySQL database is often one of the most commonly used data stores in many applications. In MySQL, data paging query is a common data retrieval operation. This article will introduce how to use Go language to implement high-performance MySQL data paging query. 1. What is data paging query? Data paging query is a commonly used data retrieval technology, which allows users to browse only a small amount of data on a page without loading it all at once.

Paging query skills for PHP and Oracle database Paging query skills for PHP and Oracle database Jul 11, 2023 pm 11:09 PM

Paging query techniques for PHP and Oracle database When developing a dynamic web page, if you need to display a large amount of data, you need to perform paging query. Paginated query is a technique that divides data into smaller pages so that users can browse and navigate easily. In this article, we will discuss how to implement paginated queries using PHP and Oracle database, and provide relevant code examples. 1. Preparation Before starting, we need to make sure that we have installed and configured PHP and Oracle database. If still

How to use Mysql for paging query in ThinkPHP6 How to use Mysql for paging query in ThinkPHP6 Jun 20, 2023 pm 02:01 PM

With the rapid development of the Internet, the development of web applications is becoming more and more complex. And paging query is one of the common functions in web applications. ThinkPHP6 is a web framework that helps developers develop applications quickly. In this article, we will discuss how to perform paginated queries using MySQL in ThinkPHP6. First, we need to create the database in ThinkPHP6. The statement to create a database in MySQL is as follows: CREATEDATABASE

How to use MongoDB with PHP for paginated query How to use MongoDB with PHP for paginated query Jul 07, 2023 pm 09:28 PM

Overview of how PHP uses MongoDB for paginated queries: MongoDB is a non-relational database often used to store large amounts of document data, while PHP is a popular server-side scripting language. In this article, we will introduce how to use PHP to connect to MongoDB and implement paging query function. Step 1: Install the MongoDB extension. To interact with MongoDB in PHP, you need to install the MongoDB extension. The MongoDB extension can be installed with the following command: p

How to use thinkorm to easily implement paging query function How to use thinkorm to easily implement paging query function Jul 31, 2023 pm 10:41 PM

Overview of how to use thinkorm to easily implement paging query function: When developing a website or application, it is often necessary to perform paging query on the data in the database in order to display part of the data on the page and provide page turning function. This article will introduce how to use the thinkorm framework to easily implement the paging query function, and provide relevant code examples. Step 1: Install thinkorm First, you need to install the thinkorm framework in the project. You can use the following command to install it: composerrequi

MySql paging query: How to deal with performance issues of paging in large databases MySql paging query: How to deal with performance issues of paging in large databases Jun 15, 2023 pm 03:28 PM

In modern applications, most data needs to be displayed in pages. When applications need to process large amounts of data, this puts pressure on servers and databases, causing queries to take longer. Mysql is currently one of the most popular relational databases. This article will discuss how to optimize the performance of Mysql paging queries. Principle of paging Before starting optimization, we must first understand the principle of paging. The main principle of paging query is to divide the table data into several pages, and then query the data one by one. For example, if we need to

See all articles