gpt4 book ai didi

Mysql程序自增变量

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:23 24 4
gpt4 key购买 nike

我正在尝试使用存储时间获取表中的用户排名。

原始 SQL 查询工作正常,但我无法使其作为过程工作。

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

我的尝试过程:

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

最佳答案

您需要继续使用 9.4. User-Defined Variables , 不是 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

关于Mysql程序自增变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20353534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com