---
title: "mysql 数据库分页逻辑"
author: "monkeycn"
date: "2011-05-20T09:19:51Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2011-05-20T09:19:51Z"
id: 1051230
url: "https://monkeycn.iteye.com/blog/1051230"
---

mysql 数据库分页逻辑

// 基于0,  返回0：错误, 否则返回总页数
uint get_recs_on_page_of(int page_index, int n_per_page)
{
     int n = select  count(*) from books;
     int page_count;
     page_count = n/page_count;
     n?(page_count += n%page_count?1:0): page_count=1;
     
     if ( page_index >= page_count ) return 0;
     int offset = page_index*n_per_page;
     print (select * from table where ... limit offset, n_per_page);
     return page_count;
}