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.设置定时任务
我这里设置的是每五分钟执行一次
MySQL update case select 存储过程 设置定时任务_第1张图片

在定义里面设置调用存储过程
MySQL update case select 存储过程 设置定时任务_第2张图片

你可能感兴趣的:(mysql基本操作)