简体   繁体   English

为什么 PostgreSQL 中的 LIMIT 和 OFFSET (OFFSET...ROWS FETCH FIRST...ROW only) 函数不起作用?

[英]Why LIMIT и OFFSET (OFFSET ... ROWS FETCH FIRST ... ROW only) functions in PostgreSQL are not working?

I am trying to use LIMIT and OFFSET functions or OFFSET... ROWS FETCH FIRST... ROW only.我正在尝试使用 LIMIT 和 OFFSET 函数或 OFFSET...ROWS FETCH FIRST...仅行。 PostgreSQL gives me the wrong number of rows in result. PostgreSQL 结果中的行数错误。

select user_id, max(order_ts) as lastorder
from production.orders
group by user_id 
order by lastorder desc, user_id desc
OFFSET 10 ROWS 
FETCH FIRST 20 ROW only

or或者

select user_id, max(order_ts) as lastorder
from production.orders
group by user_id 
order by lastorder desc, user_id desc
OFFSET 10 
limit 20 

Still gives me 20 rows (should be 10: from 10th row to 20th - is 10).仍然给我 20 行(应该是 10:从第 10 行到第 20 行 - 是 10)。

How is this?这怎么样? Any help, please?有什么帮助吗?

LIMIT 20 tells server to return not more than 20 records. LIMIT 20告诉服务器返回不超过 20 条记录。 FETCH FIRST 20 ONLY is absolutely the same. FETCH FIRST 20 ONLY是完全一样的。 The query might return 20 rows or less depending on the data and query conditions.根据数据和查询条件,查询可能返回 20 行或更少。 If you are trying to get rows from 11th to 20th then you need to specify LIMIT 10 OFFSET 10 .如果您尝试获取第 11 到第 20 行,则需要指定LIMIT 10 OFFSET 10

See the paragraph LIMIT Clause in the documentation for details: https://www.postgresql.org/docs/15/sql-select.html#SQL-LIMIT有关详细信息,请参阅文档中的LIMIT 子句段落: https://www.postgresql.org/docs/15/sql-select.html#SQL-LIMIT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OFFSET N FETCH FIRST M ROWS与JDBC和PostgreSQL无法正常工作 - OFFSET N FETCH FIRST M ROWS with JDBC and PostgreSQL not working PostgreSQL - 从 LIMIT OFFSET 重复行 - PostgreSQL - repeating rows from LIMIT OFFSET PostgreSQL LIMIT with OFFSET 不能超过 100000 - PostgreSQL LIMIT with OFFSET is not working beyond 100000 为什么我的带有 OFFSET/FETCH 的 SQL 语句不起作用? - Why is my SQL statement with OFFSET/FETCH not working? 奇怪的行为偏移…下一个行捕获…仅在ORDER BY之后行 - Strange behavior OFFSET … ROWS FETCH NEXT … ROWS ONLY after ORDER BY “OFFSET 0 ROWS FETCH Next 10 ROWS only”的性能问题 - Performance issue with "OFFSET 0 ROWS FETCH Next 10 ROWS only" 在分页的同时获取所有页面的第一行(LIMIT / OFFSET) - Get first row of all pages while also paginating (LIMIT / OFFSET) 如何在具有 LIMIT 和 OFFSET 的一系列行之间获取随机行? - How to get an random row between a range of rows with LIMIT and OFFSET? LIMIT 和 OFFSET 视图上的 ORDER BY 行 - ORDER BY rows on view for LIMIT and OFFSET PostgreSQL 中的分组限制:显示每个组的前 N ​​行,但仅当这些行的第一行等于特定数据时 - Grouped LIMIT in PostgreSQL: show the first N rows for each group, BUT only if the first of those row equals specific data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM