本文主要是介绍sql批量更新的方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
业务场景:更新多条数据某一相同字段的值
sql:PGSQL(参考的是mysql的写法,一样可用)
参考链接1:MySQL批量更新数据
参考链接2:Mysql Update批量更新的几种方式
我的实现方式Mybatis:
<update id="updateXxx" parameterType="java.util.List">update Aset field = CASE id<foreach collection="list" item="item" index="index" open="" separator="" close="">when #{item.id} THEN #{item.field}</foreach>ENDwhere id in<foreach collection="list" item="item" index="index" open="(" separator="," close=")">#{item.id}</foreach></update>
解释:通过匹配相同id的形式,更新A表中field这个字段的值。
这篇关于sql批量更新的方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!