我的问题与此类似: Need a row count after SELECT statements: What's the best SQL method?
但我想从查询中获取行总数,然后使用 limit 来创建分页,这样我就无法使用返回的行。从一个简单的查询:
select * from res_groups
Run Code Online (Sandbox Code Playgroud)
我进入了这个:
select a.*, (select count(1) from (select * from res_groups) e) total
from (select * from res_groups) a limit 10 offset 10;
Run Code Online (Sandbox Code Playgroud)
或者我可以使用简单的方法并进行两个查询:
select * from res_groups limit 10;
select count(*) from res_groups;
Run Code Online (Sandbox Code Playgroud)
第一个查询是否具有性能?我担心来自 res_groups 的查询会被执行两次?
还有其他办法吗?ps:我使用的是postgres,我知道mysql有FOUND_ROWS()