其实,你不需要使用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