1. In the actual application scenario, we will use: if the data is in the table, update the data; if the data does not exist, insert the data.
In oracle, you can use the merge into implementation, and in mysql, you can use the ON DUPLICATE KEY UPDATE. Here, only the merge into implementation method in oracle is introduced. The sql syntax is as follows:
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;(If testtable Is an empty table. After executing the above statement, it is as follows:) id A B --------------- 1 2 1 select * from testtable;(If you do it again merge into,testtable Result) id A B --------------- 1 2 2
Interpretation:
using dual on (...) is a fixed writing method. It can also be written as using (select 1 from dual) on (...), and the table name dual can also be written as other names.
on(...) is a condition. You can write a condition, or you can use and to connect multiple conditions.
2. By the way, I will introduce how to use merge into in mybatis to directly access the code, which uses dynamic sql.
<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>
Note: be sure to use the update tag.