用mysql存储过程创建分页

时间:2013-11-18 08:54:17

标签: php mysql stored-procedures pagination

M尝试使用存储过程创建分页,

当我尝试将1作为参数传递时,它将记录0到14 当我作为参数传递2时它给出0-30, 当我将2作为参数传递时,我想要15-30

create PROCEDURE abc(pageno INT)
BEGIN  
DECLARE page_no int;
DECLARE rstarts int;
DECLARE rends int; 
DECLARE userPage int;

set @userPage = pageno;

PREPARE STMT FROM 
" SELECT * FROM primaryinfo LIMIT ?,? "; 
SET @START =(@userPage - 1) * 15; 
SET @LIMIT = @userPage * 15; 
EXECUTE STMT USING @START, @LIMIT;
DEALLOCATE PREPARE STMT;
END

还是有其他解决办法有效地做到这一点.. 提前谢谢

1 个答案:

答案 0 :(得分:2)

没有陈述的分页:

create PROCEDURE test(
  IN first_rec integer,
  IN rec_count integer
)
BEGIN
  -- return --
  SET @rownum=0;
  SELECT * FROM (
    SELECT
    user.*, @rownum:=@rownum+1 AS rn FROM user
  ) t WHERE rn>=first_rec and rn<first_rec+rec_count;
END;;