【MySQL】mysql分页调用
存储过程:
CREATE DEFINER=`root`@`%` PROCEDURE `Proc_PageCondition`(p_cloumns varchar(500), p_tables varchar(400), p_where varchar(4000), p_order varchar(100), p_pageindex int, p_pagesize int, out p_recordcount int, out p_pagecount int) begin declare v_sqlcounts varchar(4000); declare v_sqlselect varchar(4000); set v_sqlcounts = concat('select count(*) into @recordcount from ',p_tables,p_where); set @sqlcounts = v_sqlcounts; prepare stmt from @sqlcounts; execute stmt; deallocate prepare stmt; set p_recordcount = @recordcount; set p_pagecount = ceiling((p_recordcount+0.0)/p_pagesize); if p_pageindex <1 then set p_pageindex = 1; end if; set v_sqlselect = concat('select ',p_cloumns,' from ',p_tables,p_where,if(p_order is not null,p_order,''),' limit ',(p_pageindex-1)*p_pagesize,' , ',p_pagesize); set @sqlselect = v_sqlselect; prepare stmtselect from @sqlselect; execute stmtselect; deallocate prepare stmtselect; end
调用过程:
set @p_cloumns = ' tr.id trainID,te.id eventID,u.uid userID,u.username,te.EventTime,te.EventLength,te.EventResult,tt.MaxSpeed,tt.MaxRotateSpeed,tt.ForeOrBack,te.EventStartFrameNumber,te.EventEndFrameNumber '; set @p_tables = ' train_event te left join train_track tt on te.trackID=tt.id left join users u on u.uid=tt.TrainnerID left join training_record tr on te.TrainID=tr.id '; set @p_where = ' where 1=1 '; set @p_order = ' order by te.EventTime desc '; set @p_pageindex = 1; set @p_pagesize = 10; set @p_recordcount = 0; set @p_pagecount = 0; call rigour_tennis_db.Proc_PageCondition(@p_cloumns, @p_tables, @p_where, @p_order, @p_pageindex, @p_pagesize, @p_recordcount, @p_pagecount); select @p_cloumns, @p_tables, @p_where, @p_order, @p_pageindex, @p_pagesize, @p_recordcount, @p_pagecount;
注意:
起始页为pageIndex由1开始