[英]In Oracle PL/SQL can I swap the value of the same column of two differents rows in a table when the rest of the values of the columns are equal?
基本上我在為某些行分配值時錯了。 兩行在所有列中具有相同的值但只有一行,並且在該列中應該交換值。 我該如何處理這個問題?
我已經嘗試過一些代碼,但只有當其他值之一不同才能將其用作參考時,它才有效。 但是當所有其他值相同時,它將無法工作。
在'bar'列中,我只想交換兩行中的值。
foo bar foo2
1 2 4
1 3 4
您可以使用CASE語句執行此操作:
update the_table
set the_column = case pk_column
when 1 then (select the_column from the_table where pk_column = 2)
when 2 then (select the_column from the_table where pk_column = 1)
end
where pk_column in (1,2);
以上假設pk_column
被定義為唯一的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.