mergeinto products p using (select*from newproducts) np on (p.product_id = np.product_id) when matched then updateset p.product_name = np.product_name where np.product_name like'OL%' --这里表示只是对product_name开头是'OL'的匹配上的进行update,如果开头不是'OL'的就是匹配到了也不会做操作
当然,这个where条件如果直接放在using语句中效果也是一样的。
同样的,insert语句里也可以使用where,比如:
1 2 3 4 5
mergeinto products p using (select*from newproducts) np on (p.product_id = np.product_id) when matched then updateset p.product_name = np.product_name where np.product_name like'OL%' whennot matched then insertvalues(np.product_id, np.product_name, np.category) where np.product_name like'OL%'
mergeinto products p using (select*from newproducts) np on (p.product_id = np.product_id) when matched then updateset p.product_name = np.product_name deletewhere p.product_id = np.product_id where np.product_name like'OL%' whennot matched then insertvalues(np.product_id, np.product_name, np.category) --这里的结果是会把匹配的记录的prodcut_name更新到product里,并且把product_name开头为OL的删除掉。
mybatis中批量merge into
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<updateid="mergeinto"> merge into user_type a using ( <foreachcollection="list"item="item"separator="union all"> select #{item.name,jdbcType=VARCHAR} as name, #{item.type,jdbcType=VARCHAR} as type from dual </foreach> ) b on (a.type = b.type) when matched then update set name = #{name} when not matched then insert (type,name) values(#{type},#{name}) </update>