Pregunta

Estoy tratando de obtener un rango de usuario en la mesa con tiempo almacenado.

La consulta SQL RAW funciona bien, pero no puedo hacer que funcione como procedimiento.

SET @rownum := 0;
SELECT rank, user_id, best_time
FROM (
  SELECT @rownum := @rownum +1 AS rank,id, best_time, user_id
  FROM user_round WHERE round_id=1 ORDER BY best_time ASC
) AS result WHERE user_id = 1 

Mi intento de procedimiento:

BEGIN
 DECLARE variable INT DEFAULT 0;
 SELECT rank,best_time, user_id
 FROM (
  SELECT SET variable=variable+1 AS rank, best_time, user_id
  FROM database.user_round WHERE round_id=1 ORDER BY best_time ASC
 ) AS result WHERE user_id = 1;
END
¿Fue útil?

Solución

Necesitas continuar usando un 9.4. Variables definidas por el usuario, No un 13.6.4.1. Variable local Declarar sintaxis:

BEGIN
    -- DECLARE variable INT DEFAULT 0;
    SELECT rank, best_time, user_id
    FROM (
        -- SELECT SET variable = variable + 1 AS rank, best_time, user_id
        SELECT @variable := @variable + 1 AS rank, best_time, user_id
        FROM database.user_round, (SELECT @variable := 0) init
        WHERE round_id = 1
        ORDER BY best_time ASC
    ) AS result
    WHERE user_id = 1;
END
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top