亲宝软件园·资讯

展开

MySQL实现数据批量更新功能详解

做猪呢,最重要的是开森啦 人气:0

根据不同条件批量更新同一值

这种场景直接通过update语句更新即可,如:

UPDATE t_sys_user SET `desc` = 'CaseWhen-0' WHERE id IN (1001,1002,1003);

根据不同条件更新不同值

循环遍历逐条更新

foreach多条更新:

<update id="updateForeach">
    <foreach collection="list" item="param" separator=";">
        UPDATE t_sys_user
        <set>
            <if test=" param.desc != null and param.desc != ''">
                `desc` = #{param.desc}
            </if>
        </set>
        WHERE id = #{param.id}
    </foreach>
</update>

case when 进行数据批量更新:

<update id="updateBatchCaseWhen">
    UPDATE t_sys_user
    <set>
        <trim prefix="`DESC`= CASE id" suffix="END,">
            <foreach collection="list" item="param">
                <if test="param.id != null">
                    WHEN #{param.id} THEN #{param.desc}
                </if>
            </foreach>
        </trim>
<!--            <trim prefix="username = CASE id" suffix="END,">
            <foreach collection="list" item="param">
                <if test="param.username != null and param.username != ''">
                    WHEN #{param.} THEN #{param.username}
                </if>
            </foreach>
        </trim> 	
-->
    </set>
    <where>
        id in
        <foreach collection="list" item="param" separator="," open="(" close=")">
            #{param.id}
        </foreach>
    </where>
</update>

加载全部内容

相关教程
猜你喜欢
用户评论