存储过程和MySQL变量

时间:2016-03-30 08:17:23

标签: mysql stored-procedures

您好我正在尝试创建一个将ansID作为参数获取的过程,并更新用户表和答案表中的“得分”字段。

DELIMITER $$
CREATE PROCEDURE UpVoteAnswer(IN _ansID INT)
BEGIN
SET @_userID = SELECT user_id from Answers where id = _ansID;
UPDATE Answers SET score = score + 1 where id = _ansID;
UPDATE Users SET score = score + 1 where id = @_userID;
END
$$
DELIMITER ;



Error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT user_id from Answers where id = _ansID;
UPDATE Answers SET score = score' at line 3

1 个答案:

答案 0 :(得分:1)

使用

SET @_userID = (SELECT user_id from Answers where id = _ansID);

SELECT user_id from Answers where id = _ansID INTO @_userID;
相关问题