gpt4 book ai didi

mysql - 在oracle中的同一列中交换名称

转载 作者:行者123 更新时间:2023-11-29 01:17:01 25 4
gpt4 key购买 nike

在一个表中有数据。像这个

ID      NAME
1 Apple
2 Apple
3 Apple
4 orange
5 orange
6 orange
7 Apple
8 Apple
9 Apple
10 orange
11 orange
12 orange

数据可能超过1000次。现在需要将 apple 交换/更改/更新为 orange 和 orange 为 apple。

最佳答案

您可以在 update 语句中使用 case 来执行此操作:

update t
set name = (case when name = 'Apple' then 'Orange'
when name = 'Orange' then 'Apple'
end)
where name in ('Apple', 'Orange');

这是标准 SQL,适用于 MySQL 和 Oracle。

如果您不需要实际更改名称,而只是在 select 中交换它,则执行查询中的逻辑:

select (case when name = 'Apple' then 'Orange'
when name = 'Orange' then 'Apple'
else name
end) as name
from t;

关于mysql - 在oracle中的同一列中交换名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18356184/

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