PostgreSQL FETCH

The LIMIT statement is often used in many relational databases, such as MySQL, H2, and HSQLDB.
But the LIMIT statement does not belong to the SQL standard.
PostgreSQL provides a SQL standard statement to replace LIMIT, which is the FETCH statement, which was proposed in the SQL:2008 standard.

FETCH syntax

OFFSET start { ROW | ROWS }
FETCH { FIRST | NEXT } [ row_count ] { ROW | ROWS } ONLY

FIRST and NEXT are synonyms, ROW and ROWS are synonyms. Synonyms can replace each other.
start must be 0 or a positive integer. If there is no OFFSET clause, start defaults to 0. If start is greater than the number of rows in the result set, the query statement does not return result.
row_count is an integer greater than or equal to 1, if you do not specify it, it defaults to 1

Because the order of rows stored in the table is unspecified, you should always use the FETCH clause and order BY clause to make the order of rows in the returned result set consistent.

Note that in SQL:2008, the OFFSET clause must be before the FETCH clause. However, in PostgreSQL, the OFFSET and FETCH clauses can appear in any order.

The FETCH clause is functionally equivalent to the LIMIT clause. If you plan to make your application compatible with other database systems, you should use the FETCH clause because it follows standard SQL.

to sum up

Try to use the FETCH statement instead of the LIMIT statement.

Guess you like

Origin blog.csdn.net/weixin_42072754/article/details/109635764