mysql命令根据条件
从其他列生成列loan_amount | installment | start_date | status
------------------------------------------------------------------
500 | 100 | 2018-1-1 |
if(loan_amount%installment!= 0)then month_required to pay = loan_amount / installment + 1 else month_required = loan_amount / installment;
然后我想通过将month_required添加到start_date进行检查(如果它已超过当前日期)。如果没有,则状态将生成“不完整”,否则为“完成”。
delimiter //
set @month = "0";
create trigger `status_check` before insert on `loan`
for each row
begin
if NEW.loan_amount% NEW.installment != 0
then set @month NEW.loan_amount/ NEW.installment +1;
else set @month = NEW.loan_amount/NEW.installment;
end if ;
if NOW() <= dateadd(NEW.start_date,INTERVAL @month MONTH)
then set NEW.status = "incomplete";
else set NEW.status = "complete";
end if;
end;
//
DELIMITER ;
这里的错误是什么?请帮忙。
答案 0 :(得分:0)
我无法提供真正的答案,因为您没有提供所有数据库字段或数据样本,但某些BEFORE INSERT
和BEFORE UPDATE
触发器可能会完成这项工作。
这是BEFORE INSERT
:
DELIMITER //
DROP TRIGGER IF EXISTS `trg_test_insert`;
//
CREATE TRIGGER `trg_test_insert`
BEFORE INSERT ON `table`
FOR EACH ROW
BEGIN
IF NEW.loan_amount % NEW.installment != 0 THEN
SET NEW.month_required = NEW.loan_amount / NEW.installment + 1;
ELSE
SET NEW.month_required = NEW.loan_amount / NEW.installment;
END IF;
END;
//
DELIMITER ;
查看MySQL 5.7 Reference Manual / ... / Trigger Syntax and Examples
并在: MYSQL Triggers - how to store the result of a calculated field