如何在PostgreSQL查询中添加LIMIT子句?

时间:2018-11-14 09:29:51

标签: postgresql

我想在PostgreSQL查询中添加LIMIT子句,但它给我错误

SELECT complaint_id FROM complaint_details_v2 a where a.road_dept SIMILAR TO 'PWDBnR' order by a.server_time desc LIMIT 0, 10

以下是错误:

ERROR: LIMIT #,# syntax is not supported
SQL state: 42601
Hint: Use separate LIMIT and OFFSET clauses.
Character: 87

2 个答案:

答案 0 :(得分:1)

对于上面的示例-跳过“ 0”位

SELECT complaint_id FROM complaint_details_v2 a where a.road_dept SIMILAR TO 
'PWDBnR' order by a.server_time desc LIMIT 10

LIMIT 0、10不是postgres方言,请使用OFFSET。例如,如果要下10个结果:

SELECT complaint_id FROM complaint_details_v2 a where a.road_dept SIMILAR TO 
'PWDBnR' order by a.server_time desc OFFSET 10 LIMIT 10

http://www.sqlines.com/postgresql/limit_offset

答案 1 :(得分:0)

查询应用于选择前10行

SELECT complaint_id FROM complaint_details_v2 a where a.road_dept SIMILAR TO 'PWDBnR' order by a.server_time desc LIMIT 10

如果要在y条记录之后选择x行(从0开始计数),则应使用

SELECT complaint_id FROM complaint_details_v2 a where a.road_dept SIMILAR TO 'PWDBnR' order by a.server_time desc LIMIT x OFFSET y

希望这会有所帮助!