SQL Limit Statement

To retrieve a part of rows returned query, use LIMITand OFFSETclauses. The following illustrates the syntax of these clauses:

SELECT 
    column_list
FROM
    table1
ORDER BY column_list
LIMIT row_count OFFSET offset;

In this syntax,

  • row_countDetermine the number of rows to be returned.
  • OFFSETClause skip before starting offset row return line. OFFSETClause is optional. If you use LIMITand OFFSETclauses, OFFSETwill LIMITskip line before the offset constraint rows.

The following example to explain the above statement:

In use LIMITwhen clause, use ORDER BYclause ensures that rows are returned by the specified order is very important. The following example is a skip three rows, 4 rows and then returns the result.

note:

Not all database systems support LIMITclause, therefore, LIMITclause can be used only in certain database systems such as MySQL , PostgreSQL , SQLite , Sybase SQL Anywhere and HSQLDB.

 SQL LIMIT clause example

如下面的employees表To demonstrate LIMITclause usage.

The following statement returns the employeestable in terms of first_nameall the rows of columns to sort.

SELECT 
    employee_id, first_name, last_name
FROM
    employees
ORDER BY first_name;

Implementation of the above query, the following results -

+-------------+------------+-----------+
| employee_id | first_name | last_name |
+-------------+------------+-----------+
|         103 | Alexander  | Lee       |
|         115 | Alexander  | Su        |
|         114 | Avg        | Su        |
|         193 | Britney    | Zhao      |
|         104 | Bruce      | Wong      |
... ...
|         100 | Steven     | Lee       |
|         203 | Susan      | Zhou      |
|         106 | Valli      | Chen      |
|         206 | William    | Wu        |
+-------------+------------+-----------+
40 rows in set

If you want to only return to the previous 5line, use the LIMITclause, such as the following statement.

SELECT 
    employee_id, first_name, last_name
FROM
    employees
ORDER BY first_name
LIMIT 5;

Implementation of the above query, the following results -

+-------------+------------+-----------+
| employee_id | first_name | last_name |
+-------------+------------+-----------+
|         115 | Alexander  | Su        |
|         103 | Alexander  | Lee       |
|         114 | Avg        | Su        |
|         193 | Britney    | Zhao      |
|         104 | Bruce      | Wong      |
+-------------+------------+-----------+
5 rows in set

To skip the next two rows and acquires five elements, use LIMITand OFFSETclauses, as in the following statement.

SELECT 
    employee_id, first_name, last_name
FROM
    employees
ORDER BY first_name
LIMIT 5 OFFSET 3;

The results shown below -

If you are using MySQL, you can use the LIMIT OFFSETshort form clause.

SELECT 
    employee_id, first_name, last_name
FROM
    employees
ORDER BY first_name
LIMIT 3 , 5;

 

Reference: https: //www.yiibai.com/sql/sql-limit.html
 

Published 272 original articles · won praise 19 · views 20000 +

Guess you like

Origin blog.csdn.net/hello_cmy/article/details/104746371