How to create a unique number for unique values ​​in MySQL?

I am stuck in creating unique numbers for unique values. I have 25 rows in a MySQL table. I want to give him a rank. Suppose that 10 users received the same values: I want to assign them the same number, and the rest will be unique. I have the following MySQL query:

(SELECT std_login_id AS ID, STD_NAME, TOTAL_MARKS, (@rnk := @rnk + 1) AS RANK FROM 
(SELECT ob.std_login_id, SUM(ob.marks_obtain) AS TOTAL_MARKS, std.student_name AS STD_NAME
    FROM exam_marks_obt ob
    JOIN ac_std_preadmission STD ON ob.std_login_id = std.std_login_id
    JOIN adm_class AS cl ON cl.class_id = std.class_id
    JOIN adm_section se ON se.section_id=std.section_id
    WHERE cl.class_id = 1 AND se.section_id = 1
    GROUP BY ob.std_login_id
    ORDER BY ABS(SUM(ob.marks_obtain)) DESC)
ob CROSS JOIN (SELECT @rnk := 0) AS flyRank )
;

The following are the results:

ID | STD_NAME | TOTAL_MARKS | RANK
1  | name1    | 250         | 1
2  | name2    | 250         | 2
3  | name3    | 200         | 3
4  | name4    | 200         | 4
5  | name5    | 150         | 5
6  | name6    | 150         | 6

I want to generate a Rank, like this for 250 => 1 and 200 => 2, and 150 => 3. Please tell me, what can I do in my code to create this rank?

+4
source share
2 answers

, . , , , .

SELECT std_login_id AS ID, STD_NAME,
       (@rnk := IF(@track = TOTAL_MARKS , @rnk, @rnk + 1)) AS RANK,
       (@track:= TOTAL_MARKS) as TOTAL_MARKS
  FROM ( select *
           from testtable
       ORDER BY total_marks DESC) ob
CROSS JOIN (SELECT @rnk := 0, @track := 0) AS starter;

, , . , , , .: -)

, :

create table testtable (
    std_login_id int(11) primary key auto_increment,
    total_marks int(11),
    std_name  varchar(10)
);

insert into testtable (total_marks,std_name) values (10,'name1');
insert into testtable (total_marks,std_name) values (15,'name2');
insert into testtable (total_marks,std_name) values (15,'name3');
insert into testtable (total_marks,std_name) values (40,'name4');
insert into testtable (total_marks,std_name) values (50,'name5');
insert into testtable (total_marks,std_name) values (15,'name6');
insert into testtable (total_marks,std_name) values (10,'name7');
insert into testtable (total_marks,std_name) values (20,'name8');
insert into testtable (total_marks,std_name) values (10,'name9');
insert into testtable (total_marks,std_name) values (10,'name10');
insert into testtable (total_marks,std_name) values (50,'name11');
insert into testtable (total_marks,std_name) values (10,'name12');
insert into testtable (total_marks,std_name) values (25,'name13');
insert into testtable (total_marks,std_name) values (10,'name14');
insert into testtable (total_marks,std_name) values (50,'name15');
insert into testtable (total_marks,std_name) values (10,'name16');
insert into testtable (total_marks,std_name) values (50,'name17');
insert into testtable (total_marks,std_name) values (20,'name18');
insert into testtable (total_marks,std_name) values (40,'name19');
insert into testtable (total_marks,std_name) values (30,'name20');
+2

, .

(SELECT TOTAL_MARKS, (@rnk := @rnk + 1) AS RANK 
FROM 
(
    (SELECT distinct(TOTAL_MARKS) AS TOTAL_MARKS 
    FROM
        (SELECT SUM(ob.marks_obtain) AS TOTAL_MARKS
        FROM exam_marks_obt ob
        JOIN ac_std_preadmission STD ON ob.std_login_id = std.std_login_id
        JOIN adm_class AS cl ON cl.class_id = std.class_id
        JOIN adm_section se ON se.section_id=std.section_id
        WHERE cl.class_id = 1 AND se.section_id = 1
        GROUP BY ob.std_login_id
        ORDER BY ABS(SUM(ob.marks_obtain)) DESC)) ob 
    CROSS JOIN (SELECT @rnk := 0) AS flyRank ))

- :

| TOTAL_MARKS | RANK
| 250         | 1
| 200         | 2
| 150         | 3

STD_NAME TOTAL_MARK s.

+1

Source: https://habr.com/ru/post/1529628/


All Articles