MySQL中的批量修改、插入操作数据库

 更新时间:2023年09月18日 09:48:41   作者:Asurplus  
在平常的项目中,我们会需要批量操作数据库的时候,例如:批量修改,批量插入,那我们不应该使用 for 循环去操作数据库,这样会导致我们反复与数据库发生连接和断开连接,影响性能和增加操作时间,所以可以使用SQL 批量修改的方式去操作数据库,感兴趣的朋友一起学习下吧

在平常的项目中,我们会需要批量操作数据库的时候,例如:批量修改,批量插入,那我们不应该使用 for 循环去操作数据库,这样会导致我们反复与数据库发生连接和断开连接,影响性能和增加操作时间

所以我们可以使用编写 SQL 批量修改的方式去操作数据库

1、批量修改

UPDATE check_order_pl_detail
SET remarks =
CASE
        id
        WHEN 1 THEN '备注1'
        WHEN 2 THEN '备注2'
    END,
    retail_unit_price =
CASE
        id
        WHEN 1 THEN 100
        WHEN 2 THEN 200
    END
WHERE
    id IN ( 1, 2 )

我们去修改 check_order_pl_detail 表时,我们需要根据 id 去修改 remarks 和 retail_unit_price ,我们可以采用 CASE WHEN 的方式,去修改数据

2、批量插入

INSERT INTO user_info_backup ( `name`, age, sex, log_time) 
SELECT
    `name`,
    age,
    sex,
    DATE_SUB( curdate( ), INTERVAL 1 DAY ) AS log_time
FROM
    user_info

当我们在备份 user_info 表的数据时,我们需要将 user_info 的数据查询出来,在插入到 user_info_backup 表中

补充:

mysql 批量新增,修改

<strong>批量新增</strong>
<insert id="insertList">
  insert into sea_user (id, username,`time`)
  values
  <foreach collection="list" item="item" index="index" separator=",">
    (replace(uuid(),"-",""), #{item.username}, #{item.time})
  </foreach>
</insert>

注释

list  传过来的集合对象

item="item"  "item" 遍历的集合中的每个对象
---------------------------------------------------------------------------------------------------------
<strong>新增返回主键id (useGeneratedKeys="true" keyProperty="id")</strong>
<insert id="insert" parameterType="com.mmall.pojo.Shipping" useGeneratedKeys="true" keyProperty="id">
  insert into mmall_shipping (id, user_id, receiver_name, 
    receiver_phone, receiver_mobile, receiver_province, 
    receiver_city, receiver_district, receiver_address, 
    receiver_zip, create_time, update_time
    )
  values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{receiverName,jdbcType=VARCHAR}, 
    #{receiverPhone,jdbcType=VARCHAR}, #{receiverMobile,jdbcType=VARCHAR}, #{receiverProvince,jdbcType=VARCHAR}, 
    #{receiverCity,jdbcType=VARCHAR}, #{receiverDistrict,jdbcType=VARCHAR}, #{receiverAddress,jdbcType=VARCHAR}, 
    #{receiverZip,jdbcType=VARCHAR}, now(), now()
    )
</insert>
---------------------------------------------------------------------------------------------------------
<strong>批量修改
list  - 传过来的数据集合,使用注解
goods_id 表中数据
goodsId 对应的实体类属性
</strong>
<update id="updateBatchStock">
  update
    goods
  set
    goods_stock =
    <foreach collection="list" item="item" index="index" separator=" " open="case goods_id" close="end">
        when #{item.goodsId} then goods_stock - #{item.quantity}
    </foreach>
    ,goods_sales_quantity =
    <foreach collection="list" item="item" index="index" separator=" " open="case goods_id" close="end">
        when #{item.goodsId} then goods_sales_quantity + #{item.quantity}
    </foreach>
    where goods_id in
    <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
        #{item.goodsId}
    </foreach>
</update>

到此这篇关于MySQL中的批量操作(修改,插入)的文章就介绍到这了,更多相关MySQL批量修改内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论