【发布时间】:2021-07-13 07:59:25
【问题描述】:
我有 Oracle 11g。我想为两个不同的where 条件更新两列。
例如
update table
set col1 = 'name' where col2= '100'
col3 = 'sal' where col4 = 'id';
有可能吗?
【问题讨论】:
我有 Oracle 11g。我想为两个不同的where 条件更新两列。
例如
update table
set col1 = 'name' where col2= '100'
col3 = 'sal' where col4 = 'id';
有可能吗?
【问题讨论】:
看起来像CASE:
update your_table set
col1 = case when col2 = '100' then 'name' end,
col3 = case when col4 = 'id' then 'sal' end;
您发布的示例并未说明如果 col2 和 col4 不包含这些值该怎么办。此外,如果没有 where 子句,您将更新整个表,因此 也许 它(update 语句)缺少额外的
where col2 = '100'
and col4 = 'id'
正如你所说,你想改变性别。
update your_table set
gender = case when gender = 'male' then 'female'
when gender = 'female' then 'male'
end;
或者,decode(假设该表中只有 male 和 female 性别):
update your_table set
gender = decode(gender, 'male', 'female', 'male');
【讨论】:
where 子句进行更新,else 分支中应该有相同的列:set col1 = case when col2 = '100' then 'name' else col1 end