sql >> Database >  >> RDS >> Mysql

Een record met hetzelfde gewicht rangschikken in MySQL

Giorgos' viool gebruiken...

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

of

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



  1. mysql-tabel met 40+ kolommen

  2. Hoe python mysqldb te gebruiken om veel rijen tegelijk in te voegen

  3. Datums effectief converteren tussen UTC en lokale (dwz PST) tijd in SQL 2005

  4. Implementeer MySQL Server + DB met .Net-toepassing