Php mssql database paging SQL statement

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.