2012-04-10 77 views
3

我有以下代碼MySQL事件每年的運行計算

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年

回答

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 
+0

所以你說的答案從@jeffery_the_wind你上面的不管用? – dames 2012-04-10 14:45:22

+0

我不認爲他說了一些與我不同的東西... – Vikram 2012-04-10 15:34:41

+0

我在@jeffery_the_wind的答案中得到以下錯誤,即時試圖找到錯誤,但它給了我一個溫暖的時間,請如果我能再次請求一些幫助# 1064 - 你的SQL語法錯誤;檢查對應於你的MySQL服務器版本正確的語法使用手動附近「如果DATEDIFF(日期(NOW()),commit_date)> = 365那麼如果new.app_loan_type =「」在第7行 – dames 2012-04-10 16:19:36

0

我不認爲你可以使用在創建事件語法使用的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|| 
+0

即時得到以下錯誤,即時通訊試圖找到錯誤,但它給了我一個溫暖的時候,請我是否可以尋求一些幫助再次 #1064 - 你在你的SQL語法錯誤;檢查對應於您的MySQL服務器版本的權利手冊(date(now()),commit_date)> = 365 then if new.app_loan_type =「'at line 7 – dames 2012-04-10 15:47:43

+0

@dames - 抱歉更新需要在if語句中,檢查我的再回答。 – 2012-04-10 17:29:08

+0

@dames - 同樣,我意識到if ...然後是不必要的,您應該在更新語法的WHERE子句中添加條件。請參閱上面的新創建事件語法,並讓我知道它是否工作。 – 2012-04-10 17:49:31