In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains the "use function to achieve MySQL query line number method", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in-depth, together to study and learn "use function to achieve MySQL query line number method"!
Mysql5 uses functions to query line numbers in MySQL, which is similar to the use of variables.
The purpose of the following MySQL query line number procedure is to get a result set of the number of rows contained in the integer, similar to ROWNUM () in other DBMS. We need a user variable to hold the result after each call to rno (), so name it @ rno.
CREATEFUNCTIONrno () RETURNSINTBEGINSET@rno=@rno+1;RETURN@rno;END
We get the number of rows through the SELECT of the rno () method. Here is the result of the calling program:
Mysql > SET@rno=0;//QueryOK,0rowsaffected (0.00sec) mysql > SELECTrno (), S1 direction 2FROMt 5rowsinset + | rno () | S1 | S2 | +-+ | 1 | 1 | a | 2 | 2 | b | 3 | 3 | c | 4 | 4 | d | 5 | 5 | e | +-+ 5rowsinset (0.00sec)
The trick of setting @ rno to zero in SELECT is to use WHERE's evaluation function, which may be lost in future MySQL.
CREATEFUNCTIONrno_reset () RETURNSINTEGERBEGINSET@rno=0;RETURN1;END;SELECTrno (), s1meme s2FROMtWHERErnostatic reset () = 1bang /
Examples are as follows:
Mysql > usedb5Databasechangedmysql > delimiter//mysql > CREATEFUNCTIONrno ()-> RETURNSINT- > BEGIN- > SET@rno=@rno+1;- > RETURN@rno;- > END;//QueryOK,0rowsaffected (0.42sec) mysql > set@rno=0;- > / / QueryOK,0rowsaffected (0.00sec) mysql > selectrno (), s1fromtMaxima. | rno () | S1 | +-+-+ | 1 | 6 | 2 | 3 | 0 | 4 | 5 | 19 | 6 | 6 | 1 | 7 | 2 | 8 | 3 | 9 | 4 | 10 | 10 | 0 | 11 | 1 | 12 | 2 | 13 | 4 | +-+-- + 13rowsinset (0.06sec) mysql > CREATEFUNCTIONrno_reset ()-> RETURNSINTEGER- > BEGIN- SET@rno=0;- > RETURN1 -> END;//QueryOK,0rowsaffected (0.01sec) mysql > selectrno (), s1fromtwhererno_reset () = 1Hangxanghangmaowei + | rno () | S1 | +-+-+ | 1 | 6 | 2 | | 3 | 0 | 4 | 19 | 5 | 19 | 6 | 1 | | 7 | 2 | 8 | 3 | 9 | 4 | 10 | 0 | 11 | 1 | 12 | 2 | 13 | 4 | +-+-+ 13rowsinset (0.13sec)
Thank you for your reading, the above is the "use of functions to achieve MySQL query line number method" content, after the study of this article, I believe you have a deeper understanding of the use of functions to achieve MySQL query line number method, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.