在有时候我们需要往表中插入某条数据,但是如果数据库中存在该条数据,我们就不插入或是我们可以选择更新它,merge的用法就基于此
merge要诀:有则更新,无则添加
merge的基本格式如下
merge into tablename t1 using tablename t2 on (t1.name = t2.name)
when matched then
update set t1.name = t2.name
when not matched then
insert values(t1 .name, t2.age, t2.sex)