mysql stored procedure paging incoming where

/*
--Name: MYSQL version query paging stored procedure by peace 2013-8-14 --Input
parameter: @fields --The fields to be queried are separated by commas
--Input parameter: @tables --The table to be
queried- -Input parameter: @where -- query condition
--input parameter: @orderby -- sort field
--output parameter: @page -- the current page count starts from 1
--output parameter: @pagesize -- the size of each
page-- Output parameters: @totalcount -- total records
-- output parameters: @pagecount -- total pages
*/
DROP PROCEDURE IF EXISTS Query_Pagination;
CREATE PROCEDURE Query_Pagination
(
    in _fields varchar(2000),  
    in _tables text,
    in _where varchar(2000 ), 
    in _orderby varchar(200),
    in _pageindex int,
    in _pagesize int,
    in _sumfields  varchar(200),/*增加统计字段2013-5-8 peaceli*/
    out _totalcount int ,
    out _pagecount int
)
begin
   set @startRow = _pageSize*(_pageIndex -1);
   set @pageSize = _pageSize;  set @rowindex = 0;
     set @strsql = CONCAT('select sql_calc_found_rows @rowindex:=@rowindex+1 as rownumber,',_fields,' from ',_tables,case ifnull(_where,'') when '' then '' else concat(' where ',_where) end,' order by ',_orderby,' limit ',@startRow,',',@pageSize);
     prepare strsql from @strsql;
     execute strsql;
   deallocate prepare strsql;
   set _totalcount = found_rows();

   if (_totalcount <= _pageSize) then
                    set _pagecount = 1;
                    else if (_totalcount % _pageSize > 0) then
                    set _pagecount = _totalcount / _pageSize + 1;
                    else
                     set _pagecount = _totalcount / _pageSize;
            end if;
        end if;
if(ifnull(_sumfields,'') <> '') then set @sumsql = contact('select ',_sumfields,' from ',_tables,case ifnull(_where,'') when '' then '' else concat(' where ',_where) end); prepare sumsql from @sumsql; execute sumsql; deallocate prepare sumsql; end if; end

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326582553&siteId=291194637