作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作的程序如下:
delimiter //
CREATE PROCEDURE processPayment(in id int(11), status int(11))
BEGIN
declare msg_a text DEFAULT "Payment was marked as sent";
declare msg_d text DEFAULT "Payment was marked as denied and user balance was updated";
declare msg_w text DEFAULT "Wrong payment status";
declare msg_u text DEFAULT "Wrong payment ID";
declare amount varchar(255) DEFAULT 0;
amount = "SELECT amount FROM payments WHERE id = id";
user_id = "SELECT userid FROM payments WHERE id = id";
IF(SELECT COUNT(*) FROM payments WHERE id = id AND status = status) = 0
THEN
SELECT msg_u;
END IF;
IF(status = 0)
THEN
SELECT msg_w;
END IF;
IF(status = 1)
THEN
UPDATE payments SET status = status WHERE id = id;
SELECT msg_a;
END IF;
IF(status = -1)
THEN
UPDATE payments SET status = status WHERE id = id;
UPDATE balances SET actual = actual+amount WHERE user_id = user;
SELECT msg_d;
END IF;
END
但是当我尝试将其添加到数据库中时出现错误: 你的语法有问题 near '= "SELECT amount FROM payments WHERE id = id"; user_id = "SELECT userid FROM pa' 第 8 行我需要将查询值分配给变量。我怎样才能做到这一点?在此先感谢您的帮助。
最佳答案
您有两种方法可以实现这一目标。首先使用 SET :
SET amount = (SELECT amount FROM payments WHERE id = id);
SET user_id = (SELECT userid FROM payments WHERE id = id);
或者使用 SELECT INTO 语法:
SELECT amount INTO amount FROM payments WHERE id = id;
SELECT userid INTO user_id FROM payments WHERE id = id;
您甚至可以使用第二种方法在一行中实现它:
SELECT amount, userid INTO amount, user_id FROM payments WHERE id = id;
关于MySQL 将查询值赋给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35187553/
我是一名优秀的程序员,十分优秀!