mysql事件运行年度计算

时间:2012-04-10 12:37:34

标签: 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年

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我认为您不能为create事件语法中使用的timestamp使用动态值。而是每天运行您的事件,并且您可以在事件中使用更新查询的where子句中的datediff函数来检查应更新哪些数据。您还可以检查这样的确切日期:where day(now()) = day(commit_date) and month(now()) = month(commit_date) and year(now()) = year(commit_date) + 1;,但是在跳跃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||