其實,你不需要使用case
聲明:
update royalties set royalty = royalty * 1.2
where royalty >= 0.16;
update royalties set royalty = royalty * 1.2
where royalty >= 0.11 and royalty < 0.16;
update royalties set royalty = royalty * 1.1
where royalty < 0.11;
(事務控制之下,如果你需要原子)。如果你的問題狀態與你的問題狀態有相同的倍數,你可以將前兩個結合起來。
它的工作原理是確保先執行更高的值並限制where
子句中哪些行受到影響。
如果你覺得你必須使用一個case
聲明:
update royalties set royalty =
case when royalty >= 0.16 then royalty * 1.2
case when royalty >= 0.11 and royalty < 0.16 then royalty * 1.2
case when royalty < 0.11 then royalty * 1.1
end;
只需更改你拉什麼表了(而不是改變表本身),並將其與當前的比較:
select title_id, lorange, hirange, royalty,
case when royalty >= 0.16 then royalty * 1.2
case when royalty >= 0.11 and royalty < 0.16 then royalty * 1.2
case when royalty < 0.11 then royalty * 1.1
end as new_royalty
from royalties;
這兩個最後的「IF:」可以摺疊成單一的條件,因爲它們是不透明的......只是一個註釋 – AtliB 2011-11-03 13:34:05