MySQL sort and add serial numbers, fractions are the same and parallel

Source: Internet
Author: User

1, sort, add serial number according to total score

Table structure:

Sql:

SET @row = 0;
SELECT b.*, @row: [Email protected]+1 rownum from (
Select *,sum (a.achievement) as Achievementall from CP_CJ as a
where 1=1 GROUP by A.studentnum Order by SUM (a.achievement) DESC limit 0,30
) as B

Results:

2, sort, increase the serial number according to the total score, and in the same

Sql:

Select C.*, Ifnull ((
Select COUNT (*) from
(
Select *,sum (a.achievement) as Achievementall from CP_CJ as a
where 1=1 GROUP by A.studentnum Order by SUM (a.achievement) DESC
) as B
where C.achievementall < B.achievementall

), 0) +1 as rownum from
(
Select *,sum (a.achievement) as Achievementall from CP_CJ as a
where 1=1 GROUP by A.studentnum Order by SUM (a.achievement) DESC limit 0,30
) as C

Results:

MySQL sort and add serial numbers, fractions are the same and parallel

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.