3

我有以下代码

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 是 'Computer Loan' 或 'miscellaneous Loan' 则计算年利息 我还计算月利息、季度利息和欠款总额,但我的主要问题是如何设置 commit_date 的间隔和当前日期为 1 年

4

2 回答 2

0

我不认为您可以timestamp在创建事件语法中使用动态值。而是每天运行您的事件,您可以使用datediff事件中更新查询的 where 子句中的函数来检查应该更新哪些数据。你也可以像这样检查确切的日期:where day(now()) = day(commit_date) and month(now()) = month(commit_date) and year(now()) = year(commit_date) + 1;,但你不会在leap eyar的情况下更新,所以你应该使用这样的东西:

delimiter ||

CREATE EVENT myevent2
ON SCHEDULE EVERY 1 DAY
STARTS now()
DO
    UPDATE lms.loan SET app_yearly_intrst = (app_ln_amnt *(computer_interest/100)) where app_loan_type = 'Computer Loan' and datediff(date(now()),commit_date) >= 365;
    UPDATE lms.loan SET app_yearly_intrst = (app_ln_amnt *(miscellaneous_interest/100)) where app_loan_type = 'Miscellaneous Loan' and datediff(date(now()),commit_date) >= 365;
    UPDATE lms.loan SET app_yearly_intrst = (app_ln_amnt *(motor_vehicle_interest/100)) where app_loan_type = 'Motor Vehicle Loan' and datediff(date(now()),commit_date) >= 365;
    UPDATE lms.loan SET app_yearly_intrst = (app_ln_amnt *(mv_insurance_interest/100)) where app_loan_type = 'Motor Vehicle Insurance Loan' and datediff(date(now()),commit_date) >= 365;
    UPDATE lms.loan SET app_yearly_intrst = (app_ln_amnt *(mv_repair_interest/100)) where app_loan_type = 'Motor Vehicle Repair Loan' and datediff(date(now()),commit_date) >= 365;
    UPDATE lms.loan SET app_yearly_intrst = (app_ln_amnt *(salary_advance_interest/100)) where app_loan_type = 'Salary Advance Loan' and datediff(date(now()),commit_date) >= 365;
    UPDATE lms.loan SET app_yearly_intrst = (app_ln_amnt *(tertiary_interest/100)) where app_loan_type = 'Tertiary Loan' and datediff(date(now()),commit_date) >= 365;
    UPDATE lms.loan SET app_mnthly_intrest = (app_yearly_intrst/12), app_quarterly_intrest = (NEW.app_mnthly_intrest * 3), app_amnt_owed = (app_ln_amnt+ app_yearly_intrst) where datediff(date(now()),commit_date) >= 365;
END||
于 2012-04-10T14:07:45.727 回答
0

我不确定您是否可以在STARTS子句中使用列值,但是有一种解决方法,您可以每天安排此事件,并在您拥有的所有子句中进行额外检查,检查和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
于 2012-04-10T14:02:30.123 回答