However, I found some records on the Internet and found that they all have a feature-that is, the total number of records cannot be transferred. I just want to study it myself. Finally, it was achieved, and the efficiency may not be very good, but I also think it is good. Paste the Code directly: it is also a record for you to learn mysql.
Copy codeThe Code is as follows:
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; -- total number of records
SET @ limitStart = (m_pageNo-1) * m_perPageCnt;
SET @ limitEnd = m_perPageCnt;
SET @ sqlCnt = CONCAT ('select count (1) into @ pageCnt from ', m_table); -- this statement is critical to obtain the total value.
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