当前位置:Gxlcms > 数据库问题 > SQL(1) 批量处理

SQL(1) 批量处理

时间:2021-07-01 10:21:17 帮助过:32人阅读

update id="updateSortBatch" parameterType="java.util.List"> <foreach collection="list" item="banner"> update t_banner_record set sort_id = #{banner.sortId} where id=#{banner.id} </foreach> </update>

(2)使用case when实现

实际的sql格式

UPDATE course
    SET name = CASE id 
        WHEN 1 THEN name1
        WHEN 2 THEN name2
        WHEN 3 THEN name3
    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="updateSortBatch" parameterType="java.util.List">
    update t_banner_record
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="sort_id=case" suffix="end,">
        <foreach collection="list" item="banner" index="index">
          when id=#{banner.id} then #{banner.sortId}
        </foreach>
      </trim>
    </trim>
    where id in
    <foreach collection="list" item="banner" index="index" separator="," open="(" close=")">
      #{banner.id}
    </foreach>
  </update>

 

SQL(1) 批量处理

标签:span   pre   when   批量更新   multi   批量处理   end   name   font   

人气教程排行