SoFunction
Updated on 2025-03-09

php mssql database pagination SQL statement

When we write MIS systems and web applications, we all involve interaction with the database. If the amount of data in the database is large, retrieving all records at once will occupy a large amount of resources. Therefore, we often use the number of records that only take out the database as much data as we need, that is, paging statements. According to the content you have used, the statements that take N records from the M data in the database table are summarized as follows:
SQL Server
Start taking N records from the M record in the database table, and use the Top keyword: Note that if there is both top and order by in the Select statement, you will select from the sorted result set:
SELECT *
FROM ( SELECT Top N *
FROM (SELECT Top (M + N - 1) * FROM table name Order by primary key desc) t1 ) t2
Order by primary key asc
Example:
select * from ( select TOP pagesize * FROM ( SELECT TOP pagesize*cureentpage * from user_table ORDER BY id ASC ) as aSysTable ORDER BY id DESC ) as bSysTable ORDER BY id ASC
For example, from the table Sys_option (primary key is sys_id), the statement is as follows:
SELECT *
FROM ( SELECT TOP 20 *
FROM (SELECT TOP 29 * FROM Sys_option order by sys_id desc) t1) t2
Order by sys_id asc
Oracle Database
Searching N records from the M records in the database table
SELECT *
FROM (SELECT ROWNUM r,t1.* From Table name t1 where rownum < M + N) t2
where >= M
For example, from the table Sys_option (primary key is sys_id), the statement is as follows:
SELECT *
FROM (SELECT ROWNUM R,t1.* From Sys_option where rownum < 30 ) t2
Where >= 10
MySQL database
The simplest My sql database is to use the LIMIT function of mysql. LIMIT [offset,] rows starts to retrieve N records from M records in the database table:
SELECT * FROM Table Name LIMIT M,N
For example, from the table Sys_option (primary key is sys_id), the statement is as follows:
select * from sys_option limit 10,20