首页 > 数据库 > mysql教程 > Mysql分页存储过程_MySQL

Mysql分页存储过程_MySQL

WBOY
发布: 2016-06-01 13:12:24
原创
824 人浏览过
登录后复制

DROP PROCEDURE IF EXISTS pr_pager;CREATE PROCEDURE pr_pager(    IN    p_table_name        VARCHAR(1024),            IN    p_fields            VARCHAR(1024),            IN    p_page_size            INT,                    IN    p_page_now            INT,                    IN    p_order_string        VARCHAR(128),            IN    p_where_string        VARCHAR(1024),             OUT    p_page_count            INT                        )    NOT DETERMINISTIC     SQL SECURITY DEFINER     COMMENT '分页存储过程'    BEGIN        DECLARE m_begin_row INT DEFAULT 0;    DECLARE m_limit_string CHAR(64);            SET m_begin_row = (p_page_now - 1) * p_page_size;    SET m_limit_string = CONCAT(' LIMIT ', m_begin_row, ', ', p_page_size);        SET @COUNT_STRING = CONCAT('SELECT COUNT(*) INTO @ROWS_TOTAL FROM ', p_table_name, ' ', p_where_string);    SET @MAIN_STRING = CONCAT('SELECT ', p_fields, ' FROM ', p_table_name, ' ', p_where_string, ' ', p_order_string,m_limit_string);        PREPARE count_stmt FROM @COUNT_STRING;    EXECUTE count_stmt;    DEALLOCATE PREPARE count_stmt;    SET p_page_count = CEILING((@ROWS_TOTAL*1.0)/p_page_size);    PREPARE main_stmt FROM @MAIN_STRING;    EXECUTE main_stmt;    DEALLOCATE PREPARE main_stmt;    END;--测试call pr_pager("person","id",4,2,"order by id desc","",@page_count);select @page_count as page_count;
登录后复制


    IN    p_table_name         VARCHAR(1024),     --表名     IN    p_fields             VARCHAR(1024),     --输出的字段名     IN    p_page_size          INT,               --页容量    IN    p_page_now           INT,               --当前页     IN    p_order_string       VARCHAR(128),      --排序语句段 ,如:order by id desc      IN    p_where_string       VARCHAR(1024),     --where语句段    OUT   page_count           INT                --输出的总页数
登录后复制


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板