Mysql分页存储过程

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




/*
--输入参数:@strColumnlist        -- 要查询的字段用逗号隔开
--输入参数:@strTable        -- 要查询的表
--输入参数:@strWhere        -- 查询条件
--输入参数:@orderBy    -- 排序字段
--输入参数:@intPageNum        -- 当前页计数从1开始
--输入参数:@intPageSize    -- 每页大小
--输出参数:@totalCount -- 总记录数
--输出参数:@intPageCount  -- 总页数 
*/
CREATE DEFINER=`root`@`%` PROCEDURE `sp_page`(IN `strColumnlist` varchar(800),IN `strTable` varchar(200),IN `strWhere` varchar(800),IN `orderBy` varchar(200),IN `intPageNum` int,IN `intPageSize` int,OUT `totalCount` int,OUT `intPageCount` int)
BEGIN
   set @startRow = intPageSize*(intPageNum -1);
   set @pageSize = intPageSize;  set @rowindex = 0;
     set @strsql = CONCAT('select sql_calc_found_rows @rowindex:=@rowindex+1 as rownumber,',strColumnlist,' from ',strTable,case ifnull(strWhere,'') when '' then '' else concat(' where ',strWhere) end,' order by ',orderBy,' limit ',@startRow,',',@pageSize);
     prepare strsql from @strsql;
     execute strsql;
   deallocate prepare strsql;
   set totalCount = found_rows(); 

   if (totalCount <= intPageSize) then
                    set intPageCount = 1;
                    else if (totalCount mod intPageSize = 0) then
                    set intPageCount = totalCount div intPageSize;
                    else
                     set intPageCount = totalCount div intPageSize + 1;
            end if;
        end if;
END

注意:root为当前连接的用户,mariadb同样适用,还有查询字段不支持*的写法

原文链接:https://www.idaobin.com/archives/1918.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

75 − 66 =