Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

como obter a posição de linhas classificadas usando mysql e php


Existem duas maneiras de fazê-lo:

Método 1:
SET @i = 0;

SELECT * FROM 
scores s1 INNER JOIN (SELECT *, @i := @i + 1 AS rank FROM scores ORDER BY score DESC) AS s2 USING (id);

Método 2:
SELECT *, (SELECT COUNT(1) AS num FROM scores WHERE scores.score > s1.score) + 1 AS rank FROM scores AS s1
ORDER BY rank asc