不過在網上找了一些,發現都有一個特點——就是不能傳出總記錄數,乾脆自己研究吧。終於,算是搞出來了,效率可能不是很好,但是我也覺得不錯了。貼代碼吧直接:也算是對自己學習mysql的一個記錄。
複製代碼 代碼如下:CREATE PROCEDURE p_pageList
(
m_pageNo int ,
m_perPageCnt int ,
m_column varchar(1000) ,
m_table varchar(1000) ,
m_condition varchar(1000),
m_orderBy varchar(200) ,
out m_totalPageCnt int
)
BEGIN
SET @pageCnt = 1; -- 總記錄數
SET @limitStart = (m_pageNo - 1)*m_perPageCnt;
SET @limitEnd = m_perPageCnt;
SET @sqlCnt = CONCAT('select count(1) into @pageCnt from ',m_table); -- 這條語句很關鍵,用來得到總數值
SET @sql = CONCAT('select ',m_column,' from ',m_table);
IF m_condition IS NOT NULL AND m_condition <> '' THEN
SET @sql = CONCAT(@sql,' where ',m_condition);
SET @sqlCnt = CONCAT(@sqlCnt,' where ',m_condition);
END IF;
IF m_orderBy IS NOT NULL AND m_orderBy <> '' THEN
SET @sql = CONCAT(@sql,' order by ',m_orderBy);
END IF;
SET @sql = CONCAT(@sql, ' limit ', @limitStart, ',', @limitEnd);
PREPARE s_cnt from @sqlCnt;
EXECUTE s_cnt;
DEALLOCATE PREPARE s_cnt;
SET m_totalPageCnt = @pageCnt;
PREPARE record from @sql;
EXECUTE record;
DEALLOCATE PREPARE record;
END