mysql update case update field value is not fixed operation

mysql update case update field value is not fixed operation

When processing batch updates of certain data, if the values ​​of the fields you are updating are the same, such as a certain status is updated to a fixed value,

Just use update table set xxx=xxx where xxx=xxx

If the value of the field to be updated is not fixed, it is more convenient to use the following update case when where method:

UPDATE tablename 
set a1= CASE userid 
 WHEN 1 THEN a1+5 
 WHEN 2 THEN a1+2 
 END,
a2= CASE userid 
 WHEN 1 THEN a2-5 
 WHEN 2 THEN a2-2
END
where userid in (1,2)

When using update case when, you must include the where condition, otherwise the entire table will be updated, which can have serious consequences.

Supplement: MySQL update && case when combined with batch update

Direct code sql:

The room field value is in the format of 18F-N01

UPDATE t_report SET room = CONCAT_WS( '-', SUBSTRING_INDEX(room, '-', 1) ,
 CASE SUBSTRING_INDEX(room, '-', -1) 
 WHEN 'N01' THEN 'N02' 
 WHEN 'N02' THEN 'N01'
 WHEN 'N03' THEN 'N11'
 WHEN 'N04' THEN 'N10'
 WHEN 'N05' THEN 'N09'
 WHEN 'N06' THEN 'N08'
 WHEN 'N07' THEN 'N07'
 WHEN 'N08' THEN 'N06'
 WHEN 'N09' THEN 'N05'
 WHEN 'N10' THEN 'N03'
 END )
WHERE rid IN ( 
 SELECT rid FROM t_report WHERE rdate = '190306' AND ordinal BETWEEN '23' AND '32'
)

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Practical MySQL + PostgreSQL batch insert update insertOrUpdate
  • Difference between MySQL update set and and
  • Non-standard implementation code for MySQL UPDATE statement
  • Summary of Mysql update multi-table joint update method
  • Explanation of mysql transaction select for update and data consistency processing
  • A "classic" pitfall of MySQL UPDATE statement

<<:  Things to note when writing self-closing XHTML tags

>>:  How does Vue solve the cross-domain problem of axios request front end

Recommend

Sample code for implementing 3D rotation effect using pure CSS

Mainly use the preserve-3d and perspective proper...

How to install the latest version of docker using deepin apt command

Step 1: Add Ubuntu source Switch to root su root ...

Centos8 bridge static IP configuration method in VMware virtual machine

1. Make sure the network connection method is bri...

MySQL database operations and data types

Table of contents 1. Database Operation 1.1 Displ...

12 Laws of Web Design for Clean Code [Graphic]

Beautiful code is the foundation of a beautiful we...

JavaScript deshaking and throttling examples

Table of contents Stabilization Throttling: Anti-...

Complete steps to upgrade Nginx http to https

The difference between http and https is For some...

Database SQL statement optimization

Why optimize: With the launch of the actual proje...

Resolving MySQL implicit conversion issues

1. Problem Description root@mysqldb 22:12: [xucl]...

How to install ionCube extension using pagoda

1. First install the pagoda Installation requirem...

How to run postgreSQL with docker

1. Install Docker. Reference URL: Docker Getting ...

Detailed explanation of DIV+CSS naming rules can help achieve SEO optimization

1. CSS file naming conventions Suggestion: Use le...

VMWare15 installs Mac OS system (graphic tutorial)

Installation Environment WIN10 VMware Workstation...

JS Decorator Pattern and TypeScript Decorators

Table of contents Introduction to the Decorator P...