gpt4 book ai didi

mysql - 节省查询 sql 的时间

转载 作者:太空宇宙 更新时间:2023-11-03 11:01:22 26 4
gpt4 key购买 nike

我有一个场景。我的表中有 300 条记录。我执行查询以获取总计数。然后,因为我必须实现分页,

我根据计数使用限制从同一个表中选择数据。我在想我是否可以在单个查询中获取计数和数据。? .

我试过下面的代码:

从表中选择 * ,count(*) 作为 cnt;

但这给了我总数但只有 1 条记录!

有没有办法节省我在查询中耗尽的时间并在单个查询中获得结果?

最佳答案

类似于:

select t1.*,t2.cnt
from table t1
cross join (select count(*) as cnt from table) t2
limit 'your limit for the first page'

select *,(select count(*) from table) as cnt
from table
limit 'your limit for the first page'

关于mysql - 节省查询 sql 的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15411093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com