简体   繁体   中英

How to change one of the fields in all tables in the database to a new value?

I want to update a values of the field in all tables in the database.

For example:

I have 4 tables that they all contain 'price' fields.

old

tbl_a | tbl_b | tbl_c | tbl_d
price | price | price | price  
 5    |  5    |   6   |   9
 6    |  6    |   7   |   10

update '6' to '11'.Then

new

tbl_a | tbl_b | tbl_c | tbl_d
price | price | price | price  
 5    |  5    |  11   |   9
 11   |  11   |   7   |   10

How can I write this sql?

Write four update statement:

update tbl_a
    set price = 11
    where price = 6;

update tbl_b
    set price = 11
    where price = 6;

update tbl_c
    set price = 11
    where price = 6;

update tbl_d
    set price = 11
    where price = 6;

Although MySQL allows multi-table updates, there is no need for that mechanism. If you want all of these to take effect at the same time, you can write the update s in a transaction .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM