MySQL use LIMIT page

  Demand: The client by passing pageNum (p) and the pageSize (number per page shown) two tabs data query parameters to the database tables.
  We know that MySQL provides paging function limit m, n, but the demand and usage of this function is not the same, so we need to be rewritten to meet the needs of the actual situation. analyse as below:
  Queries Article 1 to Article 10 Data is sql: select * from table limit 0,10; -> data corresponds to our needs is a query on the first page: select * from table limit (1-1) * 10,10 ;
  Article 10 query to Article 20 is data sql: select * from table limit 10,20; -> corresponding to our needs data of the second page is to query: select * from table limit (2-1) * 10,10 ;
  Queries Article 20 to Article 30 Data is sql: select * from table limit 20,30; -> corresponds to our needs is to query data for the third page: select * from table limit (3-1) * 10,10 ;
  Through the above analysis, results can be summarized meet the needs of the pseudo code is paged SQL: select * from table limit (pageNum-1) * pageSize, pageSize. All in all, we just need to tell the database from the start to take the first few lines of how many pieces of data on the line.
  However, limit statement does not support the calculation, therefore, need (pageNum-1) * pageSize calculation result, that is calculated from the external mass participation in Java. Order rowIndex = (pageNum-1) * pageSize, rowIndex used is calculated as follows:
// 计算行号
public  static  int getRowIndex ( int pageno, int Pagesize) {
     return (pageno> 0)? (pageNum- 1) * Pagesize: 0 ; 
}
Reference:

Guess you like

Origin www.cnblogs.com/east7/p/10941584.html