Reputation: 2418
I'm trying to get rank of user in the table with stored time.
RAW SQL query is working fine but I can't make it work as procedure.
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
My try to procedure:
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
Upvotes: 0
Views: 7575
Reputation: 16551
You need to continue using a 9.4. User-Defined Variables, not a 13.6.4.1. Local Variable DECLARE Syntax:
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
Upvotes: 3