一、在實際應用場景中,咱們會用到:若是這條數據在表中,就更新數據;若是不存在這條數據,就插入這條數據。
在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