Mysql分页存储过程

记下来 以后要用直接过来copy了

http://blog.csdn.net/xugangjava


CREATE DEFINER=`root`@`localhost` PROCEDURE `pageSplit`(
  fromWhere varchar(50),
  selectColum varchar(500),
  orderBy varchar(100),
  conditions varchar(500),
  curPage int,
  pageSize int,
  OUT totalCount int)
BEGIN
 DECLARE countSQL varchar(1200);
  DECLARE selectSQL varchar(1200);
  IF conditions!='' THEN
    SET conditions=concat(' where ',conditions);
  END IF;
  
  SET countSQL=concat('select count(1) into @recordcount from ',fromWhere,conditions);
  SET @sqlCount:=countSQL;
  PREPARE stmt FROM @sqlCount;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
  SET totalCount:=@recordcount;
  IF curPage<1 THEN
    SET curPage=1;
  END IF;
  IF pageSize<1 THEN
    SET pageSize=1;
  END IF;
  IF orderBy!='' THEN
    SET orderBy=concat(' order by ',orderBy);
  END IF;


  SET selectSQL=concat('select '
  ,selectColum,
  ' from ',fromWhere
  ,conditions,' '
  ,orderBy,' limit '
  ,(curPage-1)*pageSize
  ,',',pageSize, ' ');
  SET @sqlSelect:=selectSQL;
  PREPARE stmtselect FROM @sqlSelect;
  EXECUTE stmtselect;
  DEALLOCATE PREPARE stmtselect;
END


你可能感兴趣的:(mysql,存储)