mysql分页查询时,如何正确的获取总数
1、普遍方法:
使用 COUNT(*) ,例如:
select count(*) as total from foods_info LIMIT 0,10;
select count(*) as total from foods_info;
缺点: 记录集还需要单独的查询来获取,相当于两次查询
2、推荐方法
select SQL_CALC_FOUND_ROWS * from foods_info LIMIT 0,10;
SELECT FOUND_ROWS() as total;
虽然看起来是两条SQL语句,但是实际上只执行了一次数据库查询。