首页 >数据库 >mysql教程 >导入数据时, 存在的更新, 不存在的插入

导入数据时, 存在的更新, 不存在的插入

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原创
2016-06-07 14:56:391405浏览

导入数据时, 存在的更新, 不存在的插入 无 CREATE?trigger?tr_bank3?ON?bank3AFTER?INSERTAS??????Update?bank1?set?bank1.fmoney=bank1.fmoney+c.a???????From?????????(select?fid,sum(case?when?TYPE=0?then?fmoney?when?type=1?then?-fmoney?end)?as?a?Fr

导入数据时, 存在的更新, 不存在的插入
CREATE?trigger?tr_bank3?ON?bank3
AFTER?INSERT
AS
?
?
????Update?bank1?set?bank1.fmoney=bank1.fmoney+c.a?
??????From?
????????(select?fid,sum(case?when?TYPE=0?then?fmoney?when?type=1?then?-fmoney?end)?as?a?From?bank3?group?by?Fid)?C?
????where?
????????C.FID=bank1.FID?
????????AND?EXISTS(SELECT?1?FROM?INSERTED?WHERE?Fid=bank3.Fid))
?
????insert?into?bank1?
????select?
????????fid,
????????sum(case?when?TYPE=0?then?fmoney???when?type=1?then?-fmoney?end)?as?a?
????from?
????????bank3?AS?a
????WHERE?
????????EXISTS(SELECT?1?FROM?INSERTED?WHERE?Fid=a.Fid)
????AND?
????????NOT?EXISTS(SELECT?1?FROM?bank1?WHERE?Fid=a.Fid)
????group?by?Fid
MERGE [target] t
using [source] s on t.id = s.id
when matched then update t.name = s.name, t.age = s.age -- use "rowset1"
when not matched then insert values(id,name,age) -- use "rowset2"
when source not matched then delete; -- use "rowset3"
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn