What is the best way to generate ranks in MYSQL?
Solution 1:
This will return the students' rank, student ID, and GPA.
set @rownum := 0;
SELECT @rownum := @rownum + 1 AS rank, student_id, gpa
FROM `students` ORDER BY gpa DESC
Solution 2:
This will return the rank as rownum
SELECT @rownum := @rownum + 1 rownum,
t.*
FROM (SELECT @rownum:=0) r,
(SELECT * FROM students ORDER BY gpa DESC) t;