MySQL, Oracle Paging query

Source: Internet
Author: User

  1. MySQL Paging
  2. Parameters to be required:
  3. PageSize How many data are displayed per page
  4. PageNumber pages coming from the client
  5. Total number of records in the Totalrecouds table select COUNT (*) from table name
  6. TotalPages Total Pages
  7. totalpages=totalrecouds%pagesize==0?totalrecouds/pagesize:totalrecouds/pagesize+1
  8. Pages Start location
  9. pages= pagesize* (pagenumber-1)
  10. SQL statements:
  11. SELECT * from table name limit pages, pageSize;
  12. MySQL paging relies on the keyword limit it requires two parameters: start position and pagesize
  13. Start Position = page Size * (pages-1)
  14. Starting position =pagesize* (PageNumber-1)
  15. One: Paging needs:

    The client passes the start (page number), limit (number of bars per page) two parameters to page through the data in the database table, then we know that the MySQL database provides paging function limit m,n, but the use of this function is different from our needs, So we need to adapt to the actual situation to rewrite the appropriate page for our own statement, the specific analysis is as follows:

    Like what:

    The SQL for querying data 1th through 10th is: SELECT * FROM table limit 0, 10; We need to query the first page of data: SELECT * FROM table limit (1-1) *10,10;

    The SQL for querying data 10th through 20th is: SELECT * FROM table limit 10, 20; We need to query the second page of data: SELECT * FROM table limit (2-1) *10,10;

    The SQL for querying data 20th through 30th is: SELECT * FROM table limit 20, 30; We need to query the third page of data: SELECT * FROM table limit (3-1) *10,10;

    Second: Through the above analysis, we can get to meet our own needs of the paging SQL format is: SELECT * FROM table limit (start-1) *limit,limit; Where start is the page number, limit is the number of bars displayed per page.

    1. Oracle Paging
    2. PageSize How many data are displayed per page
    3. PageNumber pages coming from the client
    4. Total number of records in the Totalrecouds table select COUNT (*) from table name
    5. TotalPages Total Pages
    6. totalpages=totalrecouds%pagesize==0?totalrecouds/pagesize:totalrecouds/pagesize+1
    7. StartPage Start position
    8. Startpage= pagesize* (pagenumber-1) +1
    9. Endpage=startpage+pagesize
    10. SQL statements
    11. Select A.* from
    12. (
    13. Select RowNum num, t.* from table name t where column = some value order by ID ASC
    14. ) A
    15. where A.num>=startpage and A.num<endpage
      1. Select t2.* from (
        Select T1.*,rownum rn from (
        Select Employee_id,emp_name,email,phone_number,hire_date,j.job_title,
        Salary,d.department_name from Employeess E
        Left join JOBSS J on e.job_id=j.job_id
        Left join DEPARTMENTSS D on e.department_id=d.department_id
        ) T1 where rownum &lt;= #{up}
        ) T2 where RN &gt; #{down}
      2. Mapper:list<employees> Findbypageno (@Param ("Up") int up, @Param (' down ') int down);
      3. Dao:
        Public list<employees> Findbypageno (int pageno, int pagenum) {
        int up=pageno*pagenum;
        int down= (pageNo-1) *pagenum;
        Return Employeemapper.findbypageno (Up,down);
        }
      4. Service:
        Public list<employees> Findbypageno (int pageno, int pagenum) {
        Return Employeedao.findbypageno (pageno,pagenum);
        }
      5. Controller
        @RequestMapping ("Employeelist.do")
        Public String list (int pageno, model model) {
        Quantity per page
        int pagenum = 3;
        Total quantity
        int count = Employeeservice.findmaxsize ();
        Total pages
        int Pagemax = count% Pagenum = = 0? Count/pagenum:count/pagenum + 1;

        System.out.println ("list---------");
        Page judgment
        if (PageNo < 1) {
        PageNo = 1;
        }
        if (PageNo > Pagemax) {
        PageNo = Pagemax;
        }

        list<employees> all = Employeeservice.findbypageno (PageNo, pagenum);
        Return the results to the page display
        Model.addattribute ("EmployeeList", all);
        Model.addattribute ("PageNo", PageNo);
        Model.addattribute ("Pagemax", Pagemax);


        return "EmployeeList";
        }
       

MySQL, Oracle Paging query

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.