MySQL get row position in ORDER BY

前端 未结 9 1244
囚心锁ツ
囚心锁ツ 2020-11-22 13:57

With the following MySQL table:

+-----------------------------+
+ id INT UNSIGNED             +
+ name VARCHAR(100)           +
+----------------------------         


        
9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 14:42

    I've got a very very similar issue, that's why I won't ask the same question, but I will share here what did I do, I had to use also a group by, and order by AVG. There are students, with signatures and socore, and I had to rank them (in other words, I first calc the AVG, then order them in DESC, and then finally I needed to add the position (rank for me), So I did something Very similar as the best answer here, with a little changes that adjust to my problem):

    I put finally the position (rank for me) column in the external SELECT

    SET @rank=0;
    SELECT @rank := @rank + 1 AS ranking, t.avg, t.name
      FROM(SELECT avg(students_signatures.score) as avg, students.name as name
    FROM alumnos_materia
    JOIN (SELECT @rownum := 0) r
    left JOIN students ON students.id=students_signatures.id_student
    GROUP BY students.name order by avg DESC) t 
    

提交回复
热议问题