码迷,mamicode.com
首页 > 数据库 > 详细

not exists优化 mysql数据存在不插入

时间:2020-04-01 17:56:29      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:modifier   sts   rom   com   mon   代码   art   stat   div   

记一次sql优化

原代码

INSERT INTO DataAnalysis_HrmDataHistory
                              (estate,lockstate,delestate,creator,created,modifier,modified,name,employeeCode,sex,department_id,place_id,onDutyTime,outDutyTime,isDimission,birthday) 
                              SELECT estate,lockstate,delestate,creator,created,modifier,modified,chineseName,employeeCode,sex,department_id,subCompany,onDutyTime,outDutyTime,isDimission,birthday 
                              FROM
                             (
                                SELECT * FROM Hrm_HrmData 
                                WHERE (isDimission = 79 AND onDutyTime < date_add(2020-0-01, interval 1 MONTH)) 
                                OR (isDimission = 80 AND outDutyTime >= date_add(2020-01-01, interval 1 MONTH))
                             ) h  
                               WHERE  NOT EXISTS  
                            (
                                SELECT * FROM DataAnalysis_HrmDataHistory WHERE DataAnalysis_HrmDataHistory.employeeCode = h.employeeCode
                            )

 

 

INSERT INTO DataAnalysis_HrmDataHistory
                              (estate,lockstate,delestate,creator,created,modifier,modified,name,employeeCode,sex,department_id,place_id,onDutyTime,outDutyTime,isDimission,birthday) 
                              SELECT estate,lockstate,delestate,creator,created,modifier,modified,chineseName,employeeCode,sex,department_id,subCompany,onDutyTime,outDutyTime,isDimission,birthday 
                              FROM
                             (
                                SELECT * FROM Hrm_HrmData 
                                WHERE (isDimission = 79 AND onDutyTime < date_add(2020-01-01, interval 1 MONTH)) 
                                OR (isDimission = 80 AND outDutyTime >= date_add(2020-01-01, interval 1 MONTH))
                             ) h  
                               WHERE h.employeeCode not in  
                            (
                                SELECT employeeCode FROM DataAnalysis_HrmDataHistory
                            )

 

not exists优化 mysql数据存在不插入

标签:modifier   sts   rom   com   mon   代码   art   stat   div   

原文地址:https://www.cnblogs.com/zxs117/p/12614156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!