In MySQL, how can I print the position of a record in the selected result set?

Source: Internet
Author: User

In MySQL, we sometimes want to select N records and add a column in each record in the results to mark the number of records.
In Oracle, rownum can be used for simple implementation. But in MySQL, how can this problem be implemented? The following is an example:
Select t. page, t. pv, @ rn: = if (1 = 1, @ rn + 1, @ rn) as rn
From
(Select
Page,
Pv
From
Module_page_statis
Where
StatisDay = '2017-05-31 'and module = 'new _ info' order by 2 desc limit 10) t, (select @ rn: = 0) t2
Order by t. pv desc


Here is a slight explanation:
1, (select @ rn: = 0) only serves as initialization.
2, @ rn: = if (1 = 1, @ rn + 1, @ rn). This statement is executed once every record is selected. Of course, more complex logic can be added here.

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.