MySQL将查询值分配给变量

时间:2016-02-03 20:57:57

标签: mysql

我制作了下面列出的程序:

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 

但是当我尝试将其添加到数据库中时,我收到错误:  您的语法有些错误' =" SELECT amount FROM payments WHERE id = id&#34 ;; user_id =" SELECT userid FROM pa'第8行 我需要将查询值分配给变量。 我怎样才能做到这一点? 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

你有两种方法来实现这一目标。首先使用SET:

let predicate = NSPredicate(format: "latitude BETWEEN {\(latitude-0.1),
\(latitude+0.1)} AND longitude BETWEEN {\(longitude-0.1),\(longitude+0.1)} 
\(test)")

或使用SELECT INTO语法:

SET amount  = (SELECT amount FROM payments WHERE id = id);
SET user_id = (SELECT userid FROM payments WHERE id = id);

您甚至可以使用第二种方法在一行中实现它:

SELECT amount INTO amount FROM payments WHERE id = id;
SELECT userid INTO user_id FROM payments WHERE id = id;