How to Use General stored procedures to perform MySQL paging queries

Source: Internet
Author: User

The following articles mainly describe general stored procedures to perform correct operations on MySQL paging queries, if you are interested in performing the correct operations on the MySQL paging query, you can click the following article to view it. I hope you will find some benefits after browsing.

Some time ago, I did not provide a general stored procedure for transferring SQLServer to MySQL (the best combination with PHP). In the spirit of sharing, I would like to dedicate this MySQL paging query common stored procedure for everyone, assume that the database 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.  
  27. If currpage = 1 then
  28. If sCondition <> ''then
  29. Set sSql = concat ('select', columns, 'from', tablename, 'where ');
  30. Set sSql = concat (sSql, sCondition, sOrder, 'limit? ');
  31. Else
  32. Set sSql = concat ('select', columns, 'from', tablename, sOrder, 'limit? ');
  33. End if;
  34. Else
  35. If sCondition <> ''then
  36. Set sSql = concat ('select', columns, 'from', tablename );
  37. Set sSql = concat (sSql, 'where', sCondition, 'and', Prima (the most perfect VM management system) ry_field, sTemp );
  38. Set sSql = concat (sSql, '(', Prima (the most perfect VM management system) ry_field, ')', 'from (select ');
  39. Set sSql = concat (sSql, '', Prima (the most perfect VM management system) ry_field, 'from', tablename, sOrder );
  40. Set sSql = concat (sSql, 'limit', (currpage-1) * pagesize, ') as tabtemp)', sOrder );
  41. Set sSql = concat (sSql, 'limit? ');
  42. Else
  43. Set sSql = concat ('select', columns, 'from', tablename );
  44. Set sSql = concat (sSql, 'where', Prima (the most complete virtual host Management System) ry_field, sTemp );
  45. Set sSql = concat (sSql, '(', Prima (the most perfect VM management system) ry_field, ')', 'from (select ');
  46. Set sSql = concat (sSql, '', Prima (the most perfect VM management system) ry_field, 'from', tablename, sOrder );
  47. Set sSql = concat (sSql, 'limit', (currpage-1) * pagesize, ') as tabtemp)', sOrder );
  48. Set sSql = concat (sSql, 'limit? ');
  49. End if;
  50. End if;
  51. Set @ iPageSize = pagesize;
  52. Set @ sQuery = sSql;
  53. Prepare stmt from @ sQuery;
  54. Execute stmt using @ iPageSize;
  55. End;
  56. $
  57. 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:

 
 
  1. 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.

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.