我的问题类似于这个问题:https://stackoverflow.com/questions/4840987/need-a-row-count-after-select-statement-whats-the-optimal-sql-approach">Need a row count after SELECT statement: what's the optimal SQL approach?
但是我想从查询中获取总行数,然后使用limit创建分页,所以我不能使用返回的行数。
从一个简单的查询开始:
select * from res_groups
我进入了这个:
select a.*, (select count(1) from (select * from res_groups) e) total
from (select * from res_groups) a limit 10 offset 10;
或者我可以采用简单的方法,进行两个查询:
select * from res_groups limit 10;
select count(*) from res_groups;
第一个查询是否高效?我担心从res_groups查询会执行两次?
还有其他方法吗? 附注:我正在使用postgres,我知道mysql有FOUND_ROWS()