使用 LIMIT 0、20 进行无限滚动 - 如何限制总结果数?

using LIMIT 0, 20 for infinite scroll - how to limit the number of total results?

我有一个问题:

 select * from table where id is not null order by desc LIMIT :start, :limit

ajax 无限滚动/分页工作的开始和限制

如果我已经对分页使用限制,我如何限制总结果?

我需要类似于下面的查询:

select * from table where id is not null order by desc LIMIT :start, :limit LIMIT 5000

不用查询也可以计算。如果您不想要超过 5000 个结果,这应该可行:

if (($start + $limit) <= 5000) {
    // Do query
}
else {
    // Don't do query, maximum limit reached
}

您可以使用以下语法在数据库中创建视图:

create view view_name as select * from table where id is not null order by desc LIMIT 5000

然后您将查询 ajax 无限滚动/分页的视图

select * from view_name where id is not null order by desc LIMIT :start, :limit

注意:对于视图,您可以使用更简单的查询,因为视图已经选择了好的行(具有非空 ID),并对它们进行排序。

select * from view_name LIMIT :start, :limit