mysql存儲過程查找某記錄處於分頁的哪一頁

最近的做些東西涉及到了,判斷用戶添加的某條記錄處於將來分頁的第幾頁,想了一下用存儲過程實現也不錯,
自己做了一個小測試,測試方法如下:

#drop procedure if exists getPageNo

create procedure getPageNo
(
  in id varchar(10)
)

begin

declare pageNo int;
declare pageSize int;
declare pageCount int;
declare startIndex int;
declare endIndex int;
declare returnNo int;
set pageSize=10;
set pageNo = 1;
set pageCount=(select ceiling(count(*)/pageSize) from article);  #get the record count
set endIndex=0;
set startIndex=(pageNo-1)*pageSize;
set endIndex=pageSize;
set @idCount=0;
loop_label:
while pageNo <= pageCount do

set @sqlcnt=concat('select count(*) into @idCount from(select orgid from article order by center,service limit ',startIndex,',',endIndex,') as tmp where

a_id=/'',id,'/'');
prepare s_cnt from @sqlcnt;
execute s_cnt;
deallocate prepare s_cnt;

if((@idCount)!=0) then
   set returnNo=pageNo;
   leave loop_label;
end if;

set pageNo = pageNo + 1;
set startIndex = (pageNo-1) * pageSize;
set endIndex = pageSize;

end while;
#show result
select returnNo ;

end;



測試存儲過程:

call getPageNo('4124');

 因爲我沒有給存儲過程返回值,所以我沒有把結果存到返回值中。

發佈了32 篇原創文章 · 獲贊 4 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章