我在交替这张表时遇到了一些麻烦:
我的表是styles
(id, style
)
id Style
------------
01 Style-01
02 Style-02
03 Style-03
04 Table
06 Chair
我需要将单词“Style-XX”更改为 Coach-XX,其中 XX 是偶数(0、2、4、6)。
所以它看起来像这样:
id Style
------------
01 Coach-02
02 Style-03
03 Coach-04
04 Table
06 Chair
好吧,我知道它必须是这样的:XX mod 2 = 0,为了使数字为偶数(0、2、4 等)。
但是我真的不知道怎么把单词分开:S
最佳答案
你的问题让我有点困惑,因为这些词与你的例子不太相符。无论如何,如何使用一个简单的 UPDATE
步骤,如下所示:
update your_table
from (
select id
, case
when Style = 'Style-01' then 'Coach-02'
when Style = 'Style-03' then 'Coach-04'
when Style = 'Style-05' then 'Coach-06'
when Style = 'Style-07' then 'Coach-08'
when Style = 'Style-09' then 'Coach-00'
end as new_style
from your_table
where Style in ('Style-01','Style-03','Style-05'
'Style-07','Style-09'
) x
set Style = x.new_style
where your_table.id = x.id
我认为明确地做事比试图解析一个值和做算术更好。如果您需要重新访问代码,这样的事情更容易遵循。
关于基于Oracle的SQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13778863/