背景:在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也就是说当存在记录时,就更新(Update),不存在数据时,就插入(Insert)。
比如现在有张J_USER这张表,
T_USER表
用 J 表 的数据 来更新 T 表的数据
MERGE INTO t_user t using (select * from j_user j) b on (t.id = b.aid)
when matched then
update set t.year = b.year
when not matched then
insert (t.id ,t.name,t.year) values(b.aid,b.name,b.year);
可以理解为 使用j_user 这张表关联, 条件是 t.id=b.aid 条件成立的时候 执行 update, 否则 insert
执行结果如下:
--------------------------------------------------------------------------------------------