gpt4 book ai didi

MySQL 将查询值赋给变量

转载 作者:行者123 更新时间:2023-11-29 06:17:12 24 4
gpt4 key购买 nike

我制作的程序如下:

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/

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