[Sql: query student exercises 16] of the total score, and rank

Topic: Query student's total score, and rank, repeat not retained when the total score ranking vacancies

Analysis: Query student's total score and write this sort sql simple, according to student student groups and direct sum (performance score)

SELECT studentid,SUM(score) AS a FROM student_score GROUP BY studentid ORDER BY a DESC

But to achieve this sort, but how to rank number one ranking to represent na?

So reference variable

SET @crank =0;
SELECT b.studentid, b.a, @crank := @crank +1 AS rank FROM
 (SELECT studentid,SUM(score) AS a FROM student_score GROUP BY studentid ORDER BY a DESC)b ;

 

 

Useful herein to a variable: SET @crank = 0;

I Baidu under the sql variable: no contact before

Custom Variables: declare variables: the SET @crank = 0variable assignment: @crank: = @crank + 1'd   , the assignment operator or =: =   used: Find, comparison operations, etc.,    Scope: for the current session ( connection) is valid, the session variable with the scope  

 

 

Guess you like

Origin www.cnblogs.com/yuanyuan2017/p/11363811.html