【问题标题】:Keyset pagination get the super last page and total record count with the same query键集分页获取同一个查询的超级最后一页和总记录数
【发布时间】:2021-09-16 19:54:06
【问题描述】:

我有一张桌子test

------------------------------
id  | date     | description
------------------------------
1   | 07/08/09 | the date no 1.
10  | 07/08/10 | the date no 2.
3   | 07/08/11 | the date no 3.
9   | 07/08/12 | the date no 4.
... | ...      | ...

我知道我可以像这样选择第一页(用于键集分页)

SQL 编号1

select t.id as id, t.date as record_date, t.description as description
from test t order by t.id LIMIT 2

需要将最后一条记录(3 | 07/08/11 | the date no 3.)保存到缓存中 用于下一页查找。

可以使用

检索下一页

SQL 编号2

select t.id as id, t.date as record_date, t.description as description
from test t where t.id > 3 order by t.id LIMIT 2

记住我的分页看起来像这样

| << | < | > | >> |

在哪里

&gt;下一页

&gt;&gt;超级最后一页

&lt;上一页

&lt;&lt;超级首页

如何选择超级最后一页的记录?

如何选择上一页的记录?

超级首页如何选择记录?

是否可以选择一个查询 当前选定的记录数 + 选定的记录 + 该查询的可用记录总数?

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    你可以这样查询最后一页:

    SELECT * FROM (
       SELECT t.id AS id,
              t.date AS record_date,
              t.description AS description
       FROM test t ORDER BY t.id DESC LIMIT 2
    ) AS subq
    ORDER BY id;
    

    上一页是通过类似的查询获取的,只是附加了一个WHERE 条件。

    但是没有快速获取总行数的方法;您必须计算和计算整个结果。我的建议不是给用户确切的计数,而是给EXPLAIN 查询并显示 PostgreSQL 的行数估计值。你可以阅读my article on the problems involved with counting。

    【讨论】:

      猜你喜欢
      • 2011-01-12
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多