簡體   English   中英

MySQL更新查詢以更新許多行中的列

[英]Mysql update query to update a column in many rows

我有一個包含兩列a和b的表。 就像是 :

     a  |  b
   +----+----+
     x  |  l
     y  |  m
     z  |  n

使用單個mysql更新查詢,我想更新許多行中的列...

update tableName set b = l1,m1,n1 where a= x,y,z respectively.

更新查詢應該是什么樣的?

編輯我的問題以使其更清楚:我不想在列值中附加1。 我想將它們更新為與舊值不同的新值。 那么有沒有辦法使用單個MYSQL查詢來做到這一點?

基本上,我想將這些查詢合並為一個:

update tableName set b=newVal, where a=something;
update tableName set b=anotherNewVal, where a=something_else;
update tableName set b=yetAnotherNewVal, where a=something_else_again;

非常感謝 !

非常感謝您的所有幫助。 @Giorgos Betsos,@mynawaz和@Mathew。 我找到了查詢!

update tableName 
set b = case 
     when a ='x' then 'a is x' 
     when a ='y' then 'a is y' 
     else b 
end;

嘗試這個

UPDATE tableName SET b = CONCAT(b, '1') WHERE a = 'x' OR a = 'y' OR a = 'z'

要么

UPDATE tableName SET b = CONCAT(b, '1') WHERE a IN ('x', 'y', 'z')
 UPDATE `tableName`
 SET `tableName`.`b`= 
 CASE 
 WHEN `tableName`.`b`='l'  THEN 'l1'
 WHEN `tableName`.`b`='m'  THEN 'm1'
 WHEN `tableName`.`b`='n'  THEN 'n1'
 END
 WHERE `tableName`.`a`  IN ('x','y','z');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM