Home > Database > Mysql Tutorial > body text

关于Mysql的数据列表的分页显示_MySQL

WBOY
Release: 2016-06-01 13:44:36
Original
1026 people have browsed it

bitsCN.com

 

网站 刚发布时没有几篇文章,首页基本上是列出全部内容,最近文章多起来了,需要弄个分页显示。

网站用的是MySQL数据库,获取某一页的文章的代码基本如下:

 

int start = countPerPage*(page-1);

执行select * from .... limit start, countPerPage

 

于分页时需要列出一些页码,这就需要获得文章总数,一般用select count(*) .......就能获取数据。这里的问题在于为了实现分页,需要对数据库进行两次查询,效率不高,所以想是不是可以用一次查询解决这个问题,上网搜了一下,果然找到答案:

mysql> SELECT SQL_CALC_FOUND_ROWS * FROM ... WHERE ... LIMIT 10;

mysql> SELECT FOUND_ROWS();

 

参数SQL_CALC_FOUND_ROWS表示查询时记录查询结果数

函数FOUNT_ROWS返回上次查询(SQL_CALC_FOUND_ROWS)的结果数

 

虽然也需要select两次,不过第2次select不需要查询记录,直接返回结果,效率可以提高一倍

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!