三款 mysql 分页存储过程实例
如果你正在mysql教程 5以上版本,我要告诉你这里有三款 mysql 分页存储过程实例哦,存储过程是mysql 5.0以后才支持的,现在看看这款存储过程吧,看一款简单存储过程
*mssql存储过程
*/
create definer=`root`@`localhost` procedure `getrecordasp教程age`(
in tbname varchar(100),
fldname varchar(100),
pagesize int,
pageindex int,
ordertype int,
strwhere varchar(2000)
)
begin
declare beginrow int;
declare sqlstr varchar(1000);
declare limittemp varchar(1000);
declare ordertemp varchar(1000);
set beginrow = (pageindex-1)*pagesize;
set sqlstr = concat('select * from ',tbname);
set limittemp = concat(' limit ',beginrow,',',pagesize);
set ordertemp = concat(' order by ',fldname);
if ordertype = 0 then
set ordertemp = concat(ordertemp,' asc ');
else
set ordertemp = concat(ordertemp,' desc ');
end if;set @sqlstring = concat(sqlstr,' ',strwhere,ordertemp,limittemp);
prepare sqlstmt from @sqlstring;
execute sqlstmt;
deallocate prepare sqlstmt;end
create definer=`root`@`localhost` procedure `getrecordcount`(
in tbname varchar(20),
in strwhere varchar(20)
)
begin
if strwhere!="" then
set @strsql=concat('select count(*) from ',tbname,' where ',strwhere);
else
set @strsql=concat('select count(*) from ',tbname);
end if;
prepare sqlstmt from @strsql;
execute sqlstmt;
deallocate prepare sqlstmt;
end
这是一款高手分享的他的存储过程
create procedure `mysqltestuser_select_pageable`(
_whereclause varchar(2000), -- 查找条件
_orderby varchar(2000), -- 排序条件
_pagesize int , -- 每页记录数
_pageindex int , -- 当前页码
_docount bit -- 标志:统计数据/输出数据
)
not deterministic
sql security definer
comment ' '
begin
-- 定义key字段临时表
drop table if exists _temptable_keyid; -- 删除临时表,如果存在
create temporary table _temptable_keyid
(
userid int
)type=heap;
-- 构建动态的sql,输出关键字key的id集合
-- 查找条件
set @sql = 'select userid from mysqltestuser ';
if (_whereclause is not null) and (_whereclause <> ' ') then
set @sql= concat(@sql, ' where ' ,_whereclause);
end if;if (_orderby is not null) and (_orderby <> ' ') then
set @sql= concat( @sql , ' order by ' , _orderby);
end if;
-- 准备id记录插入到临时表
set @sql=concat( 'insert into _temptable_keyid(userid) ', @sql);
prepare stmt from @sql;
execute stmt ;
deallocate prepare stmt;
-- key的id集合 [end]
-- 下面是输出
if (_docount=1) then -- 统计
begin
select count(*) as recordcount from _temptable_keyid;
end;
else -- 输出记录集
begin
-- 计算记录的起点位置
set @startpoint = ifnull((_pageindex-1)*_pagesize,0);
set @sql= ' select a.*
from mysqltestuser a
inner join _temptable_keyid b
on a.userid =b.userid ';set @sql=concat(@sql, " limit ",@startpoint, " , ",_pagesize);
prepare stmt from @sql;
execute stmt ;
deallocate prepare stmt;
end;
end if;drop table _temptable_keyid;
end;
下面是mysqltestuser表的ddl:
create table `mysqltestuser` (
`userid` int(11) not null auto_increment,
`name` varchar(50) default null,
`chinesename` varchar(50) default null,
`registerdatetime` datetime default null,
`jf` decimal(20,2) default null,
`description` longtext,
primary key (`userid`)
) engine=innodb default charset=gb2312;
插入些数据:
insert into `mysqltestuser` (`userid`, `name`, `chinesename`, `registerdatetime`, `jf`, `description`) values
(1, 'xuu1 ', 'www.111cn.net', '2007-03-29 12:54:41 ',1.5, 'description1 '),
- 上一篇:mysql 主从数据同步备份经验分享
- 下一篇:mysql 存储过程语法创建与查看