Assuming the table is student, the data is as follows:
We are going to implement the rank () function in MySQL, which is in-group ordering, specifically: The student table is ranked by the class (course) to the student (name) by score (score).
First create a new stored procedure realize_rank_in_mysql, with the following code:
DROP PROCEDURE IF EXISTS realize_rank_in_mysql;delimiter;; CREATE PROCEDURE realize_rank_in_mysql () BEGIN DECLARE i int; SET i = 0; While I < (select count (DISTINCT course) from student) do SET @ROW =0; INSERT into Student_rank select *, (@ROW: = @ROW + 1) as rank from student WHERE course= (SELECT DISTINCT course from S Tudent limit i,1) ORDER by score DESC; Set i = i + 1; END while; END;;D Elimiter;
Then enter the following query statement:
drop table if exists student_rank;create table Student_rank like Student;alter table Student_rank add rank int;call Realiz E_rank_in_mysql;select * from Student_rank;
The results are as follows:
Note: The rankings here do not achieve the same number of points in the same situation.