简体   繁体   中英

how to update many fields in single mysql table with “where clause”?

i have table with same fields,want to update that fields,with where clause,

update tmp_aus_inv_data
SET cust_region= "ROTOMOULD"
WHERE division = "ROTOMOULD" ;

update tmp_aus_inv_data
SET cust_region= "Internal"
WHERE division = "EXTRUSION" and ucase(cust_region) like ucase('Internal%') ;

i tried with this,but not working,

update tmp_aus_inv_data
(set cust_region= "ROTOMOULD"),
(SET cust_region= "Internal")
WHERE division = "ROTOMOULD"  and WHERE division = "EXTRUSION" and ucase(cust_region)     like ucase('Internal%') ;

You can use a CASE statement in your UPDATE , so I think you want something like this:

update tmp_aus_inv_data
SET cust_region 
    = case 
        when division = "ROTOMOULD" then "ROTOMOULD"
        when division = "EXTRUSION" 
            and ucase(cust_region) like ucase('Internal%')
          then "Internal"
        else cust_region
      end
 where division in ("ROTOMOULD", "EXTRUSION")

See SQL Fiddle with Demo

Can you please try the below query once.

UPDATE tmp_aus_inv_data
   SET `cust_region`= if(`division`= 'ROTOMOULD', 'ROTOMOULD', 'Internal') 
WHERE division in ('ROTOMOULD','EXTRUSION');

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