Oracle数据库merge into的使用,存在则更新,不存在则插入
1、在实际应用场景中,我们会用到:如果这条数据在表中,就更新数据;如果不存在这条数据,就插入这条数据。
在oracle中,可以使用merge into实现,在mysql中可以使用ON DUPLICATE KEY UPDATE,这里只介绍oracle中的merge into实现方法,sql语法如下:
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
解释:
using dual on (...)是一种固定写法,也可以写成 using (select 1 from dual) on (...) , 表名dual也可以写成其他的名字。
on(...)里面是条件,可以写一个条件,也可以用and连多个条件。
2、顺便介绍一下mybatis中怎么使用merge into,直接上代码,代码使用的是动态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>
注意:一定要用update标签。
相关推荐
敏敏张 2020-11-11
SCNUHB 2020-11-10
小木兮子 2020-11-11
wwwsurfphpseocom 2020-10-28
WasteLand 2020-10-18
Cocolada 2020-11-12
杜鲁门 2020-11-05
shirleypaddy 2020-10-19
qingmumu 2020-10-19
Testingba工作室 2020-09-15
周公周金桥 2020-09-13
专注前端开发 2020-08-16
emagtestage 2020-08-16
heniancheng 2020-08-15
hanjinixng00 2020-08-12
小方哥哥 2020-08-09
83327712 2020-07-30
卖小孩的咖啡 2020-07-21
wqiaofujiang 2020-07-05