Last active
December 11, 2015 22:58
-
-
Save fdb713/4673119 to your computer and use it in GitHub Desktop.
min stored procedure of pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- -------------------------------------------------------------------------------- | |
-- Routine DDL | |
-- Note: comments before and after the routine body will not be stored by the server | |
-- -------------------------------------------------------------------------------- | |
DELIMITER $$ | |
CREATE DEFINER=`root`@`localhost` PROCEDURE `paginate`( | |
in _table varchar(100), | |
in _where varchar(100), | |
in _order_by varchar(100), | |
in _offset int, | |
in _limit int | |
) | |
COMMENT 'paginate' | |
BEGIN | |
if _limit <= 1 | |
then | |
set _limit = 10; | |
end if; | |
if _offset < 0 | |
then | |
set _offset = 0; | |
end if; | |
set @_offset = _offset, @_limit = _limit; | |
set @stmt = concat('SELECT * FROM ', _table, ' WHERE ', _where,' ORDER BY ', _order_by,' LIMIT ', @_offset,', ', @_limit); | |
PREPARE query_stmt from @stmt; | |
EXECUTE query_stmt; | |
DEALLOCATE PREPARE query_stmt; | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment