Use variables to query row numbers in MySQL

Source: Internet
Author: User

MySQL can use variables to query the row number. Of course, if you use mysql5, you can write a function to implement it. Let's take a look at the implementation method of MySQL to query the row number.

 
 
  1. mysql> select * from t;   
  2. +------+   
  3. | s1 |   
  4. +------+   
  5. | 6 |   
  6. | 6 |   
  7. | 0 |   
  8. | 19 |   
  9. | 19 |   
  10. | 1 |   
  11. | 2 |   
  12. | 3 |   
  13. | 4 |   
  14. | 0 |   
  15. | 1 |   
  16. | 2 |   
  17. | 4 |   
  18. +------+   
  19. 13 rows in set (0.44 sec)   
  20.  
  21. mysql> set @rownum=0;   
  22. Query OK, 0 rows affected (0.00 sec)   
  23.  
  24. mysql> select @rownum:=@rownum+1 as rownum,s1 from t;   
  25. +--------+------+   
  26. | rownum | s1 |   
  27. +--------+------+   
  28. | 1 | 6 |   
  29. | 2 | 6 |   
  30. | 3 | 0 |   
  31. | 4 | 19 |   
  32. | 5 | 19 |   
  33. | 6 | 1 |   
  34. | 7 | 2 |   
  35. | 8 | 3 |   
  36. | 9 | 4 |   
  37. | 10 | 0 |   
  38. | 11 | 1 |   
  39. | 12 | 2 |   
  40. | 13 | 4 |   
  41. +--------+------+   
  42. 13 rows in set (0.04 sec)  
  43.  
  44.    
  45.  

The preceding method uses variables to query row numbers in MySQL.

Troubleshooting of MySQL query timeout

MySQL Date and Time Functions

Transformed from the MySQL Query Class of discuz

Explanation for querying cache variables in MySQL

Usage of MySQL conditional query statements

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.