Colocar un contador de registro en el select- mysql

SELECT  l.position, 
        l.username, 
        l.score,
        @curRow := @curRow + 1 AS numero
FROM    league_girl l
JOIN    (SELECT @curRow := 0) r;
Est parte JOIN (SELECT @curRow := 0) permite inicializar sin necesitar un  comando SET .
Probar:
CREATE TABLE league_girl (position int, username varchar(10), score int);
INSERT INTO league_girl VALUES (1, 'a', 10);
INSERT INTO league_girl VALUES (2, 'b', 25);
INSERT INTO league_girl VALUES (3, 'c', 75);
INSERT INTO league_girl VALUES (4, 'd', 25);
INSERT INTO league_girl VALUES (5, 'e', 55);
INSERT INTO league_girl VALUES (6, 'f', 80);
INSERT INTO league_girl VALUES (7, 'g', 15);
Consultar
SELECT  l.position, 
        l.username, 
        l.score,
        @curRow := @curRow + 1 AS row_number
FROM    league_girl l
JOIN    (SELECT @curRow := 0) r
WHERE   l.score > 50;
Resultado:
+----------+----------+-------+------------+
| position | username | score | row_number |
+----------+----------+-------+------------+
|        3 | c        |    75 |          1 |
|        5 | e        |    55 |          2 |
|        6 | f        |    80 |          3 |
+----------+----------+-------+------------+
3 rows in set (0.00 sec)

Espero les sirva
Fuente: http://stackoverflow.com/questions/3126972/mysql-row-number
Saludos
Adalberto Montanía

Comentarios