我正在使用mysql,我需要以某种方式在以后的查询中使用由prepared语句返回的curid列。我使用准备好的语句,因为如我所读,这是将变量传递给LIMIT子句的唯一方法。我在这里有此存储过程:
DROP PROCEDURE IF EXISTS fixbalance;
CREATE PROCEDURE fixbalance (userid INT)
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE balance INT DEFAULT 0;
DECLARE idcnt INT;
SET idcnt = (SELECT COALESCE(COUNT(id), 0)
FROM coupon_operations
WHERE user_id = userid);
IF idcnt <> 0 THEN
WHILE i <= idcnt DO
BEGIN
SET @iter = i;
SET @user_id = userid;
SET @sql = CONCAT('SELECT id AS curid
FROM coupon_operations
WHERE user_id = ?
ORDER BY id ASC
LIMIT ?, 1');
PREPARE stmt FROM @sql;
EXECUTE stmt USING @user_id, @iter;
DEALLOCATE PREPARE stmt;
SET balance = balance + (SELECT points
FROM coupon_operations
WHERE user_id = @user_id
AND id = @curid);
UPDATE coupon_operations SET balance = balance;
SET i = i + 1;
END;
END WHILE;
END IF;
END;
|
这是行不通的-我不知道如何通过居里。
CONCAT
在上面的代码?