Mysql
 sql >> Database >  >> RDS >> Mysql

Come classificare un record con lo stesso peso in MySQL

Usando il violino di Giorgos...

SELECT name
     , marks
     , FIND_IN_SET(marks, (SELECT GROUP_CONCAT(marks ORDER BY marks DESC) FROM mytable)) rank
  FROM mytable;

|     Name | Marks | rank |
|----------|-------|------|
| StudentA |    90 |    1 |
| StudentB |    85 |    2 |
| StudentC |    85 |    2 |
| StudentD |    70 |    4 |

http://sqlfiddle.com/#!9/7cc30/6

o

SELECT name, marks, rank
FROM (SELECT name
     , marks
     , @prev := @curr
     , @curr := marks
     , @i:[email protected]+1 temp
     , @rank := IF(@prev = @curr, @rank, @i) AS rank
  FROM mytable
     , ( SELECT @curr := null, @prev := null, @rank := 0, @i:=0) vars
 ORDER 
    BY marks DESC,name
      ) x
      ORDER 
    BY marks DESC,name

http://sqlfiddle.com/#!9/287e07/9