繁体   English   中英

如何使用“ where子句”更新单个mysql表中的许多字段?

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

我有具有相同字段的表,想通过where子句更新该字段,

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

我尝试了这个,但是没有用,

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

您可以在UPDATE使用CASE语句,所以我想您需要这样的东西:

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")

参见带有演示的SQL Fiddle

能否请您尝试以下查询一次。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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