我有三张表:
accounts: `account_id`, `donations_req`, `payments_in, balance`
donations_required: `donation_id`, `charity_id`, `account_id`, `amount`, `status`, `date`
payments: `payment_id`, `account_id`, `amount`, `date`我已经成功地汇总了每个捐赠者的所有donations_req,并覆盖了donations_req字段以及payments_in字段的付款,然后是余额(payments_in - donations_req)。
然而,我需要的是;与payments_in总数,循环通过donations_required列表(按date排序),并将那些已支付的status更改为1。
例如:如果我有三笔捐款,每笔50美元,我存入120美元。余额将是-$30,前两笔捐款应标记为已支付。
那么,考虑到donations_required表中有完全不同的帐户,我该如何标记这样的状态呢?
我甚至需要一个存储过程吗?
发布于 2013-04-30 12:20:13
您可以使用存储过程来处理它,但是您不需要循环所有的表,您只需要检查此人是否有足够的余额。如果没有,您可以更新付款id和用户id中的状态。
希望对你有帮助:) GBU
让我们假设这个人已经捐赠了三个慈善基金,可以是今天,也可以是第二天。
第一种情况,此人同时捐赠了3笔捐款,因此可以通过存储过程来检查此人是否有足够的余额,或者在这种情况下,您允许此人捐赠超过其余额。
DROP PROCEDURE IF EXISTS `checkfunds`;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `checkfunds`(IN `payment_id` VARCHAR(50), IN `account_id` VARCHAR(50), IN `amount` INT)
COMMENT 'This procedure check the Balance'
BEGIN
DECLARE balance INT;
SELECT BALANCE INTO balance from accounts where account_id = `account_id`
IF balance > 0 AND balance > 'amount' THEN
'allow'
ELSEIF balance < 0 AND balance < 'amount' THEN
'Not allow' <-- since you are here then update the status payment and your amount within your own calculation
END IF;
END//
DELIMITER ;希望能有所帮助
https://stackoverflow.com/questions/16291965
复制相似问题