Skip to content

Instantly share code, notes, and snippets.

@fdb713
Last active December 11, 2015 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdb713/4673119 to your computer and use it in GitHub Desktop.
Save fdb713/4673119 to your computer and use it in GitHub Desktop.
min stored procedure of pagination
-- --------------------------------------------------------------------------------
-- 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