Sagar Nikam
Sagar Nikam

Reputation: 1758

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%') ;

Upvotes: 0

Views: 80

Answers (2)

Taryn
Taryn

Reputation: 247840

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

Upvotes: 3

salona
salona

Reputation: 59

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');

Upvotes: 1

Related Questions