mysql Event每年运行一次计算

标签 mysql sql events

我有以下代码

CREATE EVENT myevent2
ON SCHEDULE EVERY '1' YEAR
STARTS commit_date + INTERVAL 1 YEAR
DO
UPDATE lms.loan 



        if new.app_loan_type='Computer Loan' then 
    SET NEW.app_yearly_intrst = (NEW.app_ln_amnt *(NEW.computer_interest/100));
    end if;


    if new.app_loan_type="Miscellaneous Loan" then 
    SET NEW.app_yearly_intrst = (NEW.app_ln_amnt *(NEW.miscellaneous_interest/100));
    end if;

    if new.app_loan_type="Motor Vehicle Loan" then 
    SET NEW.app_yearly_intrst = (NEW.app_ln_amnt *(NEW.motor_vehicle_interest/100));
    end if;

    if new.app_loan_type="Motor Vehicle Insurance Loan" then 
    SET NEW.app_yearly_intrst = (NEW.app_ln_amnt *(NEW.mv_insurance_interest/100));
    end if;

    if new.app_loan_type="Motor Vehicle Repair Loan" then 
    SET NEW.app_yearly_intrst = (NEW.app_ln_amnt *(NEW.mv_repair_interest/100));
    end if;


    if new.app_loan_type="Salary Advance Loan" then 
    SET NEW.app_yearly_intrst = (NEW.app_ln_amnt *(NEW.salary_advance_interest/100));
    end if;


    if new.app_loan_type="Tertiary Loan" then 
    SET NEW.app_yearly_intrst = (NEW.app_ln_amnt *(NEW.tertiary_interest/100));
    end if;

    SET NEW.app_mnthly_intrest = (NEW.app_yearly_intrst/12);
    SET NEW.app_quarterly_intrest = (NEW.app_mnthly_intrest * 3);
    SET NEW.app_amnt_owed = (NEW.app_ln_amnt+ NEW.app_yearly_intrst);

  END$$

我正在尝试创建一个事件来计算两种贷款类型的利息:'计算机贷款'和'杂项贷款'..等等,每年一次,但是每年的基础取决于 commit_date 列commit_date 列中的一年,然后如果 app_loan_type 是“计算机贷款”或“杂项贷款”,则计算年利息 我还计算月利息、季度利息和欠款总额,但我的主要问题是如何设置间隔commit_date 和当前日期为 1 年

最佳答案

我不确定您是否可以在 STARTS 子句中使用列值,但是有一个解决方法,您可以每天安排此事件并在 all 中进行额外检查 IF 子句,用于检查 commit_datecurrent_date() 之间的日期差异是一年。

类似于:

if new.app_loan_type="Salary Advance Loan" and DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), commit_date)), "%Y")+0 = 1 then

关于mysql Event每年运行一次计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10088948/

相关文章:

jQuery qTip - 创建之前触发

events - MS Access 2007 表单加载事件未触发

javascript - 存储在 javascript 变量中的 MySQL BigInt 最大值

mysql - 延迟启动 SAS EG 中的程序

php - Doctrine,symfony2 正确的列类型用于计算小时数

mysql - 在 MySql 中使用自连接到数据透视表

sql - 将 JOIN 查询重写为子查询/更快的变体

mysql - 使用delphi xe3和mysql数据库进行Sql插入

mysql - 如何将数据库从数百 MB 的大型 SQL 文件导入到 MySQL

events - OpenLayers 3 如何在 OpenLayer 2 中将功能修改事件注册为 "featuremodified"