テーブルには data.like this one があります
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 回を超える場合があります。現在、リンゴをオレンジに、オレンジをリンゴに交換/変更/更新する必要があります。
これはcase
、update
ステートメント内で次のように行います。
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;
update YourTable
set name =
case name
when 'Apple' then 'Orange'
when 'Orange' then 'Apple'
end
where name in ('Apple', 'Orange')