MySQL连表更新事件维护优化
管理员 发布于 6年前   424
优化前:
-- 备忘备注:超过账期宽限期未结款,变更机构状态“已冻结”;结清恢复; 合同授信过期按上一次合同账期,屏蔽临时授信 UPDATE bota_corper as a, ( SELECT bm.corperid,bm._corperid, sum(if(bm.settlestatus=0,1,0)) num, (select IFNULL(sum(remain),0) from bota_corper_credit as ct where ct.corperid = bm.corperid and ct.type=1 ) as ctremain FROM `bota_corper_bill_month` bm inner join `bota_corper` c on bm.corperid = c.id where date(date_add(CONCAT(bm.month,'-',c.expired),INTERVAL 1 MONTH)) < date(now()) group by corperid ) as b SET a.status = if(b.num > 0 && a.remain < 0,4,3), a._status = if(b.num > 0 && a.remain < 0,'已冻结','正常') WHERE a.id = b.corperid and b.ctremain > 0
优化后:拆分两步利于维护
-- 事件名称:机构状态自动冻结
UPDATE bota_corper as C,
(
SELECT DISTINCT
A.corperid
FROM
bota_corper_credit A,
bota_corper_bill_month B
WHERE
A.type = 1
AND
A.corperid = B.corperid
AND B.settlestatus = 0
AND B.presettle < CURDATE()
) as D
SET
C.status = 4,
C._status = '已冻结'
WHERE
C.id = D.corperid
-- 事件名称:机构状态自动解冻
UPDATE bota_corper as C,
(
SELECT DISTINCT
A.corperid
FROM
bota_corper_credit A,
bota_corper_bill_month B
WHERE
A.type = 1
AND
A.corperid = B.corperid
AND B.settlestatus = 1
) as D
SET
C.status = 3,
C._status = '正常'
WHERE
C.id = D.corperid
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成
是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客
文章标签
友情链接