postgreSQL数据库分页查询

解释:

 

 

官方解释:

语法:
LIMIT子句的SELECT语句的基本语法如下:

SELECT column1, column2, columnN
FROM table_name
LIMIT [no of rows] 
以下是LIMIT子句时使用OFFSET子句的语法:

SELECT column1, column2, columnN
FROM table_name
LIMIT [no of rows] OFFSET [row num]  
LIMIT,OFFSET允许只检索部分查询所产生的行。

 

示例:

有如下记录:

 select * from COMPANY;
 id | name  | age | address   | salary
----+-------+-----+-----------+--------
  1 | Paul  |  32 | California|  20000
  2 | Allen |  25 | Texas     |  15000
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
  6 | Kim   |  22 | South-Hall|  45000
  7 | James |  24 | Houston   |  10000
(7 rows)
下面是一个例子,这限制了表中的一行,根据从表中读取想要的行: yiibai.com

testdb=# SELECT * FROM COMPANY LIMIT 4;  
这将产生以下结果:

 id | name  | age | address     | salary
----+-------+-----+-------------+--------
  1 | Paul  |  32 | California  |  20000
  2 | Allen |  25 | Texas       |  15000
  3 | Teddy |  23 | Norway      |  20000
  4 | Mark  |  25 | Rich-Mond   |  650
(4 rows)
但在某些情况下,可能要选出一组记录从一个特定的偏移。下面是一个例子,从第三位开始取出3条:

testdb=# SELECT * FROM COMPANY LIMIT 3 OFFSET 2;  
这将产生以下结果:

id | name  | age | address   | salary
----+-------+-----+-----------+--------
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
(3 rows)

 

SQL各关键字执行顺序:

SELECT    DISTINCT   <select_list>   
FROM <left_table>
<join_type> JOIN <right_table>
ON <join_condition>
WHERE <where_condition>
GROUP BY <grout_by_list>
WITH {CUTE|ROLLUP}
HAVING <having_condition>
ORDER BY <order_by_list>
LIMIT <limit_number>
posted @ 2019-12-26 21:06  图图小淘气_real  阅读(10131)  评论(0编辑  收藏  举报