我有一个场景。我的桌子上有300条记录。我执行查询以获取总数。然后,由于我必须执行分页,

我根据计数从限制表中选择数据。我在想是否可以在单个查询中获得计数和数据。 。

我尝试下面的代码:

Select * ,count(*) as cnt from table;

但是,这给了我总数,但只有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/

10-14 13:39
查看更多