MySQL中的批量修改、插入操作數(shù)據(jù)庫
在平常的項目中,我們會需要批量操作數(shù)據(jù)庫的時候,例如:批量修改,批量插入,那我們不應(yīng)該使用 for 循環(huán)去操作數(shù)據(jù)庫,這樣會導(dǎo)致我們反復(fù)與數(shù)據(jù)庫發(fā)生連接和斷開連接,影響性能和增加操作時間
所以我們可以使用編寫 SQL 批量修改的方式去操作數(shù)據(jù)庫
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 表時,我們需要根據(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)我們在備份 user_info 表的數(shù)據(jù)時,我們需要將 user_info 的數(shù)據(jù)查詢出來,在插入到 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 - 傳過來的數(shù)據(jù)集合,使用注解
goods_id 表中數(shù)據(jù)
goodsId 對應(yīng)的實體類屬性
</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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用mysql的disctinct group by查詢不重復(fù)記錄
非常不錯的方法,用mysql的group by解決不重復(fù)記錄的問題,看來我需要學(xué)習(xí)的地方太多了2008-08-08
MySQL數(shù)據(jù)庫定時備份的幾種實現(xiàn)方法
本文主要介紹了MySQL數(shù)據(jù)庫定時備份的幾種實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07

