一、在实际应用场景中,咱们会用到:若是这条数据在表中,就更新数据;若是不存在这条数据,就插入这条数据。
在oracle中,能够使用merge into实现,在mysql中能够使用ON DUPLICATE KEY UPDATE,这里只介绍oracle中的merge into实现方法,sql语法以下:java
merge into testtable t using dual on (t.id = '1') when not matched then insert (id,a,b) values (1,2,1) when matched then update set b = b+1; select * from testtable;(若是testtable是空表,执行上述语句以后以下:) id A B --------------- 1 2 1 select * from testtable;(若是再执行一次merge into,testtable结果) id A B --------------- 1 2 2
解释:mysql
using dual on (...)是一种固定写法,也能够写成 using (select 1 from dual) on (...) , 表名dual也能够写成其余的名字。sql
on(...)里面是条件,能够写一个条件,也能够用and连多个条件。mybatis
二、顺便介绍一下mybatis中怎么使用merge into,直接上代码,代码使用的是动态sql。oracle
<update id="updateStaffEvaluation" parameterType="java.util.HashMap"> merge into staff_evaluation t using dual on (t.login_id = #{loginId,jdbcType=VARCHAR}) when not matched then insert <trim prefix="(" suffix=")" suffixOverrides="," > <if test="userId != null" > USER_ID, </if> <if test="loginId != null" > LOGIN_ID, </if> <if test="userName != null" > USER_NAME, </if> <if test="complexScore != null" > COMPLEX_SCORE, </if> <if test="mobileServiceScore != null" > MOBILE_SERVICE_SCORE, </if> <if test="broadbandScore != null" > BROADBAND_SCORE, </if> <if test="keepScore != null" > KEEP_SCORE, </if> <if test="state != null" > STATE, </if> <if test="evaluateText != null" > EVALUATE_TEXT, </if> <if test="beginTime != null" > BEGIN_TIME, </if> <if test="endTime != null" > END_TIME, </if> <if test="evaluatePerson != null" > EVALUATE_PERSON, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="userId != null" > #{userId,jdbcType=VARCHAR}, </if> <if test="loginId != null" > #{loginId,jdbcType=VARCHAR}, </if> <if test="userName != null" > #{userName,jdbcType=VARCHAR}, </if> <if test="complexScore != null" > #{complexScore,jdbcType=VARCHAR}, </if> <if test="mobileServiceScore != null" > #{mobileServiceScore,jdbcType=VARCHAR}, </if> <if test="broadbandScore != null" > #{broadbandScore,jdbcType=VARCHAR}, </if> <if test="keepScore != null" > #{keepScore,jdbcType=VARCHAR}, </if> <if test="state != null" > #{state,jdbcType=VARCHAR}, </if> <if test="evaluateText != null" > #{evaluateText,jdbcType=VARCHAR}, </if> <if test="beginTime != null" > #{beginTime,jdbcType=VARCHAR}, </if> <if test="endTime != null" > #{endTime,jdbcType=VARCHAR}, </if> <if test="evaluatePerson != null" > #{evaluatePerson,jdbcType=VARCHAR}, </if> </trim> when matched then update <set> <if test="complexScore != null" > t.COMPLEX_SCORE = #{complexScore,jdbcType=VARCHAR}, </if> <if test="mobileServiceScore != null" > t.MOBILE_SERVICE_SCORE = #{mobileServiceScore,jdbcType=VARCHAR}, </if> <if test="broadbandScore != null" > t.BROADBAND_SCORE = #{broadbandScore,jdbcType=VARCHAR}, </if> <if test="keepScore != null" > t.KEEP_SCORE = #{keepScore,jdbcType=VARCHAR}, </if> <if test="evaluateText != null" > t.EVALUATE_TEXT = #{evaluateText,jdbcType=VARCHAR}, </if> <if test="beginTime != null" > t.BEGIN_TIME = #{beginTime,jdbcType=VARCHAR}, </if> <if test="endTime != null" > t.END_TIME = #{endTime,jdbcType=VARCHAR}, </if> <if test="evaluatePerson != null" > t.EVALUATE_PERSON = #{evaluatePerson,jdbcType=VARCHAR}, </if> </set> where t.LOGIN_ID = #{loginId,jdbcType=VARCHAR} </update>
注意:必定要用update标签。ide