mysql Event running yearly calculations

后端 未结 2 712
抹茶落季
抹茶落季 2020-12-11 23:18

I have the following code

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



        if new.app_l         


        
相关标签:
2条回答
  • 2020-12-11 23:25

    i am not sure if you can use a column value in the STARTS clause, but there is a workaround for this you can schedule this event for daily and have a extra check in all the IF clause you have, to check the date difference between commit_date and current_date() is one year.

    something like:

    if new.app_loan_type="Salary Advance Loan" and DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), commit_date)), "%Y")+0 = 1 then
    
    0 讨论(0)
  • 2020-12-11 23:43

    I do not think you can use a dynamic value for the timestamp used in the create event syntax. Instead run your event everyday, and you can use the datediff function in the where clause of the update query in the event to check which data should be updated. You could also check the exact date like this: where day(now()) = day(commit_date) and month(now()) = month(commit_date) and year(now()) = year(commit_date) + 1;, but you wouldn't update in the case of leap eyar, so you should use something like this:

    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||
    
    0 讨论(0)
提交回复
热议问题