Mysql_ implementing the Rank () function in a group sort-oracle

Source: Internet
Author: User
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.

Related Article

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.