mysql插入数据两种特殊处理-指定字段存在就更新、指定字段存在就不做动作

指定字段存在就更新(UNIQUE索引或者PRIMARY KEY中出现重复值,则此时旧数据(行)执行UPDATE操作)

        INSERT INTO ha_claim_medical_billing( id, medical_billing_no, report_no, gmt_created, gmt_modified, is_deleted)
        VALUES ( #id#, #medical_billing_no#, #report_no#,  now(), now(), #is_deleted#)
        on  DUPLICATE key update
        medical_billing_no=#medical_billing_no#, report_no=#report_no#,  gmt_modified=now(), is_deleted=#is_deleted#

指定字段存在就不做动作

insert into ha_claim_medical_billing(id, medical_billing_no, report_no, gmt_created, gmt_modified, is_deleted)
        select #id#, #medical_billing_no#, #report_no#,  now(), now(), #is_deleted# from DUAL 
        where not exists (select id from ha_claim_medical_billing where id = #id#) ;

 

你可能感兴趣的:(mysql)