Use functions to query row numbers in MySQL

Source: Internet
Author: User

There are more than one implementation method for querying row numbers in MySQL. The following describes how to use functions to query row numbers in MySQL. If you are interested in this, take a look.

MySQL uses functions in MySQL 5 to query row numbers. The principle is similar to that of using variables.

The following MySQL row number query process is used to obtain the result set of the number of integer rows, similar to ROWNUM () in other DBMS (). We need a user variable to save the result after each call of rno (). Name it @ rno.

 
 
  1. CREATE FUNCTION rno ()   
  2. RETURNS INT   
  3. BEGIN   
  4. SET @rno = @rno + 1;   
  5. RETURN @rno;   
  6. END;   
  7.  

Using the SELECT method of rno (), we obtain the number of rows. The following is the result of calling the program:

 
 
  1. mysql> SET @rno = 0;//   
  2. Query OK, 0 rows affected (0.00 sec)   
  3. mysql> SELECT rno(),s1,s2 FROM t;//   
  4. +-------+------+------+   
  5. | rno() | s1 | s2 |   
  6. +-------+------+------+   
  7. | 1 | 1 | a |   
  8. | 2 | 2 | b |   
  9. | 3 | 3 | c |   
  10. | 4 | 4 | d |   
  11. | 5 | 5 | e |   
  12. +-------+------+------+   
  13. 5 rows in set (0.00 sec)   
  14.  

The trick of setting @ rno to zero in SELECT is to use the WHERE evaluate function, which may be lost in MySQL in the future.

 
 
  1. CREATE FUNCTION rno_reset ()   
  2. RETURNS INTEGER   
  3. BEGIN   
  4. SET @rno = 0;   
  5. RETURN 1;   
  6. END;   
  7. SELECT rno(),s1,s2 FROM t WHERE rno_reset()=1;//   
  8.  

Example:

 
 
  1. mysql> use db5   
  2. Database changed   
  3. mysql> delimiter //   
  4. mysql> CREATE FUNCTION rno ()   
  5. -> RETURNS INT   
  6. -> BEGIN   
  7. -> SET @rno = @rno + 1;   
  8. -> RETURN @rno;   
  9. -> END;//   
  10. Query OK, 0 rows affected (0.42 sec)   
  11.  
  12. mysql> set @rno = 0;   
  13. -> //   
  14. Query OK, 0 rows affected (0.00 sec)   
  15.  
  16. mysql> select rno(),s1 from t;//   
  17. +-------+------+   
  18. | rno() | s1 |   
  19. +-------+------+   
  20. | 1 | 6 |   
  21. | 2 | 6 |   
  22. | 3 | 0 |   
  23. | 4 | 19 |   
  24. | 5 | 19 |   
  25. | 6 | 1 |   
  26. | 7 | 2 |   
  27. | 8 | 3 |   
  28. | 9 | 4 |   
  29. | 10 | 0 |   
  30. | 11 | 1 |   
  31. | 12 | 2 |   
  32. | 13 | 4 |   
  33. +-------+------+   
  34. 13 rows in set (0.06 sec)   
  35.  
  36. mysql> CREATE FUNCTION rno_reset ()   
  37. -> RETURNS INTEGER   
  38. -> BEGIN   
  39. -> SET @rno = 0;   
  40. -> RETURN 1;   
  41. -> END;//   
  42. Query OK, 0 rows affected (0.01 sec)   
  43.  
  44. mysql> select rno(),s1 from t where rno_reset()=1;//   
  45. +-------+------+   
  46. | rno() | s1 |   
  47. +-------+------+   
  48. | 1 | 6 |   
  49. | 2 | 6 |   
  50. | 3 | 0 |   
  51. | 4 | 19 |   
  52. | 5 | 19 |   
  53. | 6 | 1 |   
  54. | 7 | 2 |   
  55. | 8 | 3 |   
  56. | 9 | 4 |   
  57. | 10 | 0 |   
  58. | 11 | 1 |   
  59. | 12 | 2 |   
  60. | 13 | 4 |   
  61. +-------+------+   
  62. 13 rows in set (0.13 sec)  
  63.  

Non-empty question in MySQL Query

Troubleshooting of MySQL query timeout

How to adjust the MySQL Query Buffer

How MySQL query optimizer works

Explanation for querying cache variables in MySQL

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.