Paginated Query Statements for MySQL, Oracle, and SQL Server

Assuming that the current page is PageNo, and each page has PageSize records, now use Mysql, Oracle, and SQL Server to query the student table by paging. 1. Mysql paging query

SELECT
     *
 FROM
     student
 LIMIT (PageNo - 1) * PageSize,PageSize;

 

Understanding: (Limit n,m) => Take m records from the nth row, and n starts from 0.

2. Oracle's paging query:

SELECT
    *
FROM
    (
        SELECT
            ROWNUM rn ,*
        FROM
            student
        WHERE
            Rownum <= pageNo * pageSize
    )
WHERE
    rn > (pageNo - 1) * pageSize

 

Understanding: Assuming pageNo = 1, pageSize = 10, first retrieve records with row numbers less than or equal to 10 from the student table, and then retrieve records with rn greater than 0 from these records to achieve paging purposes. ROWNUM starts at 1.

3. SQL Server paging query:

SELECT
    TOP PageSize *
FROM
    (
        SELECT
            ROW_NUMBER () OVER (ORDER BY id ASC) RowNumber ,*
        FROM
            student
    ) A
WHERE
    A.RowNumber > (PageNo - 1) * PageSize

 Understanding: Assuming pageNo = 1, pageSize = 10, first sort according to the id of the student table in ascending order, rownumber as the row number, and then take out 10 records starting from row 1.

  Some databases may have several methods for paging query, and the query method written here may not be the most efficient query method, but this is the most convenient paging query I use. If you are interested, you can also study other paging query methods.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326490885&siteId=291194637