1. 程式人生 > >MySQL update case select 儲存過程 設定定時任務

MySQL update case select 儲存過程 設定定時任務

delimiter //
CREATE PROCEDURE Judging_state ()
    UPDATE loan_record loan
    LEFT JOIN (
        SELECT
            id,
            total - h.c * h.months overdue,
            time,
            overdue_time
        FROM
            (
                SELECT
                    a.id id,
                    (
                        IFNULL(a.money, 0
) / IFNULL(a.repay_method_type, 3) + IFNULL(a.money, 0) * 0.1 ) c, DATEDIFF( now(), date_add( IFNULL(a.loan_time, 0), INTERVAL 3 + repay_method_type MONTH ) ) time
, DATEDIFF( now(), date_add( IFNULL(a.loan_time, 0), INTERVAL repay_method_type MONTH ) ) overdue_time, TIMESTAMPDIFF( MONTH
, IFNULL(a.loan_time, 0), curdate() ) months, IFNULL(r.money, 0) total FROM loan_record a LEFT JOIN repay_record r ON a.id = r.loan_id GROUP BY id ) h ) f ON loan.id = f.id SET loan.repay_status = ( CASE WHEN f.time >= 1 AND f.overdue < 0 THEN 5 WHEN f.overdue_time < 0 AND f.overdue > 0 THEN 1 WHEN f.overdue_time <= 0 AND f.overdue = 0 THEN 6 WHEN f.overdue_time > 0 AND f.overdue < 0 AND f.overdue_time <= 30 THEN 2 WHEN f.overdue_time > 30 AND f.overdue < 0 AND f.overdue_time <= 60 THEN 3 WHEN f.overdue_time > 40 AND f.overdue < 0 AND f.overdue_time <= 90 THEN 4 ELSE 1 END );
END ; // delimiter ;

1.建立儲存過程

2.設定定時任務
我這裡設定的是每五分鐘執行一次
這裡寫圖片描述

在定義裡面設定呼叫儲存過程
這裡寫圖片描述