When writing MIS systems and Web applications, we all involve interaction with the database. If there is a large amount of data in the database, we can retrieve all the records at a time, the system occupies a large amount of resources. Therefore, we often use paging statements to retrieve only how many records are required from the database. Based on the content you have used, the statements for retrieving N records from the M data records of Common Database SQL Server, Oracle, and MySQL are summarized as follows:
SQL Server
Obtain N records from the M records in the database table. Use the Top Keyword: Note that if the Select statement contains both top and order by, 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
Instance:
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, to retrieve 10 or 20 records from the table Sys_option (primary key: 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
Oralce Database
Retrieve 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 t2.r> = M
For example, to retrieve 10 or 20 records from the table Sys_option (primary key: sys_id), the statement is as follows:
SELECT *
FROM (select rownum r, t1. * From Sys_option where rownum <30) t2
Where t2.R> = 10
MySQL database
The simplest mysql database is to use mysql's LIMIT function, LIMIT [offset,] rows statement to retrieve N records from M records in the database table:
SELECT * FROM table name limit m, N
For example, to retrieve 10 or 20 records from the table Sys_option (primary key: sys_id), the statement is as follows:
Select * from sys_option limit 10, 20