繁体   English   中英

使用 where 条件一查询更新 mysql 表中的每一行

[英]update mysql table each row in with where condition one query

嗨,我正在尝试使用 where 条件更新 mysql 表中的每个行列

update city set (ID='1001' where ID='1') and (ID='1002' where ID='2'); 

但这似乎不起作用任何人都可以有建议吗?

就像上面的评论一样,我会这样做作为两个单独的查询:

update city set ID='1001' where ID='1'; 

update city set ID='1002' where ID='2';

使代码尽可能简单是有好处的。 以后更容易修改。

但如果你必须在一个声明中做到这一点:

update city
set ID = CASE ID WHEN '1' THEN '1001' WHEN '2' THEN '1002' END
where ID IN ('1', '2');

使用CASE表达式:

update city 
set ID = case ID
  when '1' then '1001' 
  when '2' then '1002' 
end
where ID IN ('1', '2'); 

或更简单:

update city 
set ID = ID + 1000
where ID IN ('1', '2'); 

暂无
暂无

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

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