Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to update data in batches by MySQL

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "MySQL how to update data in batches". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "MySQL how to update data in batches"!

Method 1: Case... when... then... end

MySQL implements one sql to complete multiple data updates

UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END, title = CASE id WHEN 1 THEN 'New Title 1' WHEN 2 THEN 'New Title 2' WHEN 3 THEN 'New Title 3' ENDWHERE id IN (1,2,3)

Method 2: ON DUPLICATE KEY UPDATE Usage (primary key index or unique index conflict needs to be used to decide whether to insert or update)

mysql implements upsert

insert into yundou_management.statistic_customer(customer_id,current_period,period_number,client_upload_bill,update_time) values(1,201604,100,100,1540470512),(314,201604,100,100,1540470512),(315,201604,100,100,1540470512),(316,201611,100,100,1540470512)ON DUPLICATE KEY UPDATE customer_id=values(customer_id),current_period=values(current_period),period_number=values(period_number),client_upload_bill=values(client_upload_bill),update_time=values(update_time)

Method 3: Replace into

MySQL replace into

This method has limitations: it can only be updated if there is a primary key field in the write field.

At this point, I believe that everyone has a deeper understanding of "MySQL how to update data in batches", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 215

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report