gpt4 book ai didi

MYSQL UPDATE SET 在同一列上但具有多个 WHERE 子句

转载 作者:IT王子 更新时间:2023-10-28 23:46:58 25 4
gpt4 key购买 nike

对于 MYSQL,我正在使用这个查询:

UPDATE CustomerDetails_COPY
SET Category_ID = 10
WHERE Category_ID = 2

很好,但我想向它添加 15 多个 SET/WHERE 广告,例如:

UPDATE CustomerDetails_COPY
SET Category_ID = 9 WHERE Category_ID = 3
SET Category_ID = 12 WHERE Category_ID = 4
SET Category_ID = 11 WHERE Category_ID = 5
.....

我该如何添加?

编辑:

根据猎人的建议:

UPDATE CustomerDetails_COPY
SET Category_ID = CASE Category_ID
WHEN 2 THEN 10
WHEN 3 THEN 9
WHEN 4 THEN 12
WHEN 5 THEN 11
END
WHERE Category_ID IN (2,3,4,5)

这很好用!谢谢

最佳答案

像这样的东西应该适合你:

UPDATE CustomerDetails_COPY
SET Category_ID = CASE Category_ID
WHEN 2 THEN 10
WHEN 3 THEN 9
WHEN 4 THEN 12
WHEN 5 THEN 11
END
WHERE Category_ID IN (2,3,4,5)

或者,正如 Simon 所建议的,您可以这样做以避免输入两次值:

UPDATE CustomerDetails_COPY
SET Category_ID = CASE Category_ID
WHEN 2 THEN 10
WHEN 3 THEN 9
WHEN 4 THEN 12
WHEN 5 THEN 11
ELSE Category_ID
END

来源:http://www.karlrixon.co.uk/writing/update-multiple-rows-with-different-values-and-a-single-sql-query/

关于MYSQL UPDATE SET 在同一列上但具有多个 WHERE 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9368505/

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