gpt4 book ai didi

mysql - 有没有单独的sql语句可以进行批量更新?

转载 作者:行者123 更新时间:2023-11-30 01:11:42 24 4
gpt4 key购买 nike

我只是有一个sql疑问,实际上我需要对每个颜色值多次使用更新查询...每次检查每种颜色的条件变得很困难..

是否有任何单个 sql 语句可以执行以下批量“更新”操作?请告诉我..

update `myTable` set `COLOR`='white'  WHERE `CATEGORY` = 'Dress' and `NAME` like '%white%'
update `myTable` set `COLOR`='red' WHERE `CATEGORY` = 'Dress' and `NAME` like '%red%'
update `myTable` set `COLOR`='blue' WHERE `CATEGORY` = 'Dress' and `NAME` like '%blue%'
update `myTable` set `COLOR`='pink' WHERE `CATEGORY` = 'Dress' and `NAME` like '%pink%'

最佳答案

您可以使用CASE

Update  myTable
SET COLOR = CASE
WHEN CATEGORY = 'white' AND NAME like '%white%' THEN 'white'
WHEN CATEGORY = 'red' AND NAME like '%red%' THEN 'red'
WHEN CATEGORY = 'blue' AND NAME like '%blue%' THEN 'blue'
WHEN CATEGORY = 'pink' AND NAME like '%pink%' THEN 'pink'
ELSE value
END
WHERE CATEGORY IN ('white','red','blue','pink')

关于mysql - 有没有单独的sql语句可以进行批量更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19421994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com