Mysql批量更新

Case1:更新同一字段为同一值

<update id="updateStatusByTableNameAndPartitions">
        update
            migrate_data
        set
            update_time = now(),
            migrate_status = #{migrateStatus,jdbcType=VARCHAR}
        where
            table_full_name = #{tableFullName,jdbcType=VARCHAR}
        and
            tablePartition in
        <foreach item="item" index="index" collection="partitions"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

Case2:更新同一字段为不同值

UPDATE categories 
SET display_order = CASE id 
    WHEN 1 THEN 3 
    WHEN 2 THEN 4 
    WHEN 3 THEN 5 
END, 
title = CASE id 
    WHEN 1 THEN 'New Title 1'
    WHEN 2 THEN 'New Title 2'
    WHEN 3 THEN 'New Title 3'
END
WHERE id IN (1,2,3)
<update id="updateBatch" parameterType="java.util.List">
        update adhoc_timer_task
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="next_schedule_time =case" suffix="end,">
                <foreach collection="adhocTimerTasks" item="i" index="index">
                    <if test="i.nextScheduleTime!=null">
                        when id=#{i.id} then #{i.nextScheduleTime}
                    </if>
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>

参考:

  1. https://www.jianshu.com/p/f71c88aca6fc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值