---
title: "分页存储过程个人总结"
author: "JYLike97"
date: "2007-02-11T02:55:57Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2007-02-11T02:55:57Z"
id: 53768
url: "https://jylike97.iteye.com/blog/53768"
---

初学Mysql两天内写出了分页的存储过程，代码如下：

```

CREATE DEFINER=`root`@`localhost` PROCEDURE `getAllpaged`(IN pageSize INT,IN currPage INT)
    DETERMINISTIC
BEGIN
declare Idmax int;
declare lowerbound int;
declare maxpage int;
declare upperbound int;
select max(id) from ftp into Idmax;//选出最大ID
select ceiling(1.0*Idmax/pageSize) into maxPage;//选出最后页的页号
if (currPage>0 and currPage<=maxPage) 
then
select Idmax-pageSize*currPage into lowerbound;
select Idmax-pageSize*(currPage-1) into upperbound;//实现倒选
SELECT * FROM ftp WHERE id>lowerbound AND id<=upperbound order by id desc;
else
set lowerbound=Idmax-pageSize;
SELECT * FROM ftp WHERE id>lowerbound AND id<=Idmax order by id desc;
//若在范围以外则返回第一页
end if;
END

```

<u>**后知后觉max(id)换成**count(*)好些**</u>