欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL中的批量修改、插入操作數(shù)據(jù)庫(kù)

 更新時(shí)間:2023年09月18日 09:48:41   作者:Asurplus  
在平常的項(xiàng)目中,我們會(huì)需要批量操作數(shù)據(jù)庫(kù)的時(shí)候,例如:批量修改,批量插入,那我們不應(yīng)該使用 for 循環(huán)去操作數(shù)據(jù)庫(kù),這樣會(huì)導(dǎo)致我們反復(fù)與數(shù)據(jù)庫(kù)發(fā)生連接和斷開(kāi)連接,影響性能和增加操作時(shí)間,所以可以使用SQL 批量修改的方式去操作數(shù)據(jù)庫(kù),感興趣的朋友一起學(xué)習(xí)下吧

在平常的項(xiàng)目中,我們會(huì)需要批量操作數(shù)據(jù)庫(kù)的時(shí)候,例如:批量修改,批量插入,那我們不應(yīng)該使用 for 循環(huán)去操作數(shù)據(jù)庫(kù),這樣會(huì)導(dǎo)致我們反復(fù)與數(shù)據(jù)庫(kù)發(fā)生連接和斷開(kāi)連接,影響性能和增加操作時(shí)間

所以我們可以使用編寫(xiě) SQL 批量修改的方式去操作數(shù)據(jù)庫(kù)

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 )

我們?nèi)バ薷?check_order_pl_detail 表時(shí),我們需要根據(jù) id 去修改 remarks 和 retail_unit_price ,我們可以采用 CASE WHEN 的方式,去修改數(shù)據(jù)

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

當(dāng)我們?cè)趥浞?user_info 表的數(shù)據(jù)時(shí),我們需要將 user_info 的數(shù)據(jù)查詢(xún)出來(lái),在插入到 user_info_backup 表中

補(bǔ)充:

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  傳過(guò)來(lái)的集合對(duì)象

item="item"  "item"?遍歷的集合中的每個(gè)對(duì)象
---------------------------------------------------------------------------------------------------------
<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  - 傳過(guò)來(lái)的數(shù)據(jù)集合,使用注解
goods_id 表中數(shù)據(jù)
goodsId 對(duì)應(yīng)的實(shí)體類(lèi)屬性
</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>

到此這篇關(guān)于MySQL中的批量操作(修改,插入)的文章就介紹到這了,更多相關(guān)MySQL批量修改內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論