Code Summary of MySQL paging query for common stored procedures

Source: Internet
Author: User

This article mainly tells you about the general Stored Procedure of querying MySQL by page. We did not provide the general stored procedure for transferring SQLServer to MySQL some time ago. In the spirit of sharing with you, we will provide you with this MySQL paging query General stored procedure. Assume that the database used is guestbook:

 
 
  1. Use guestbook;
  2. Delimiter $
  3. Drop procedure if exists prc_page_result $
  4. Create procedure prc_page_result (
  5. In currpage int,
  6. In columns varchar (500 ),
  7. In tablename varchar (500 ),
  8. In sCondition varchar (500 ),
  9. In order_field varchar (100 ),
  10. In asc_field int,
  11. In Prima (the most complete virtual host Management System) ry_field varchar (100 ),
  12. In pagesize int
  13. )
  14. Begin
  15. Declare sTemp varchar (1000 );
  16. Declare sSql varchar (4000 );
  17. Declare sOrder varchar (1000 );
  18.  
  19. If asc_field = 1 then
  20. Set sOrder = concat ('ORDER BY', order_field, 'desc ');
  21. Set sTemp = '<(select min ';
  22. Else
  23. Set sOrder = concat ('ORDER BY', order_field, 'asc ');
  24. Set sTemp = '> (select max ';
  25. End if;
  26. If currpage = 1 then
  27. If sCondition <> ''then
  28. Set sSql = concat ('select', columns, 'from', tablename, 'where ');
  29. Set sSql = concat (sSql, sCondition, sOrder, 'limit? ');
  30. Else
  31. Set sSql = concat ('select', columns, 'from', tablename, sOrder, 'limit? ');
  32. End if;
  33. Else
  34. If sCondition <> ''then
  35. Set sSql = concat ('select', columns, 'from', tablename );
  36. Set sSql = concat (sSql, 'where', sCondition, 'and', Prima (the most perfect VM management system) ry_field, sTemp );
  37. Set sSql = concat (sSql, '(', Prima (the most perfect VM management system) ry_field, ')', 'from (select ');
  38. Set sSql = concat (sSql, '', Prima (the most perfect VM management system) ry_field, 'from', tablename, sOrder );
  39. Set sSql = concat (sSql, 'limit', (currpage-1) * pagesize, ') as tabtemp)', sOrder );
  40. Set sSql = concat (sSql, 'limit? ');
  41. Else
  42. Set sSql = concat ('select', columns, 'from', tablename );
  43. Set sSql = concat (sSql, 'where', Prima (the most complete virtual host Management System) ry_field, sTemp );
  44. Set sSql = concat (sSql, '(', Prima (the most perfect VM management system) ry_field, ')', 'from (select ');
  45. Set sSql = concat (sSql, '', Prima (the most perfect VM management system) ry_field, 'from', tablename, sOrder );
  46. Set sSql = concat (sSql, 'limit', (currpage-1) * pagesize, ') as tabtemp)', sOrder );
  47. Set sSql = concat (sSql, 'limit? ');
  48. End if;
  49. End if;
  50. Set @ iPageSize = pagesize;
  51. Set @ sQuery = sSql;
  52. Prepare stmt from @ sQuery;
  53. Execute stmt using @ iPageSize;
  54. End;
  55. $
  56. Delimiter;

It can be stored as a database script and then imported using commands:

MySQL (best combination with PHP)-u root-p <pageResult. SQL;

Call: call prc_page_result (1, "*", "Tablename", "", "columnname", 1, "PKID", 25 );

The above content is an introduction to the MySQL paging query common stored procedures. I hope you will get some benefits.

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.