MySQL中的批量修改、插入操作數(shù)據(jù)庫
在平常的項(xiàng)目中,我們會(huì)需要批量操作數(shù)據(jù)庫的時(shí)候,例如:批量修改,批量插入,那我們不應(yīng)該使用 for 循環(huán)去操作數(shù)據(jù)庫,這樣會(huì)導(dǎo)致我們反復(fù)與數(shù)據(jù)庫發(fā)生連接和斷開連接,影響性能和增加操作時(shí)間
所以我們可以使用編寫 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 表時(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)我們在備份 user_info 表的數(shù)據(jù)時(shí),我們需要將 user_info 的數(shù)據(jù)查詢出來,在插入到 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 傳過來的集合對(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 - 傳過來的數(shù)據(jù)集合,使用注解
goods_id 表中數(shù)據(jù)
goodsId 對(duì)應(yīng)的實(shí)體類屬性
</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)文章
Ubuntu 設(shè)置開放 MySQL 服務(wù)遠(yuǎn)程訪問教程
這篇文章主要介紹了Ubuntu 設(shè)置開放 MySQL 服務(wù)遠(yuǎn)程訪問教程,需要的朋友可以參考下2014-10-10
MySQL 5.7之關(guān)于SQL_MODE的設(shè)置
這篇文章主要介紹了MySQL 5.7之關(guān)于SQL_MODE的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
MySQL如何根據(jù)不同條件聯(lián)查不同表的數(shù)據(jù)if/case
這篇文章主要介紹了MySQL如何根據(jù)不同條件聯(lián)查不同表的數(shù)據(jù)if/case問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
MySQL操作數(shù)據(jù)庫和表的常用命令新手教程
這篇文章主要介紹了MySQL操作數(shù)據(jù)庫和表的常用命令新手教程,本文總結(jié)的命令都是控制mysql必須掌握的、常用的命令,需要的朋友可以參考下2014-09-09
MySQL命令提示符出現(xiàn)輸入錯(cuò)誤時(shí)如何修改前面的命令
本文主要介紹了MySQL命令提示符出現(xiàn)輸入錯(cuò)誤時(shí)如何修改前面的命令,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10

