【问题标题】:Update two column更新两列
【发布时间】:2021-07-13 07:59:25
【问题描述】:

我有 Oracle 11g。我想为两个不同的where 条件更新两列。

例如

update table 
set col1 = 'name' where col2= '100'
    col3 = 'sal' where col4 = 'id';

有可能吗?

【问题讨论】:

    标签: sql oracle oracle11g


    【解决方案1】:

    看起来像CASE

    update your_table set
      col1 = case when col2 = '100' then 'name' end,
      col3 = case when col4 = 'id' then 'sal' end;
    

    您发布的示例并未说明如果 col2col4 不包含这些值该怎么办。此外,如果没有 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(假设该表中只有 malefemale 性别):

    update your_table set
      gender = decode(gender, 'male', 'female', 'male');
    

    【讨论】:

    • 其实gender一栏有女的换成男的,有男的换成女的
    • 你为什么不这么说?我编辑了答案并添加了更多代码。请看一下。
    • 要等于使用where 子句进行更新,else 分支中应该有相同的列:set col1 = case when col2 = '100' then 'name' else col1 end
    • 好吧,@astentx,如果这就是他们想要的。无论如何,正如 OP 所评论的那样,看起来整个问题在于“改变性别”,而不是原始示例中的那些 dummy 无关值。
    • 我做了一些很不寻常的事情。我赞成这个答案,因为它是正确的,不值得反对。我也投票结束了这个问题,因为这显然不是 OP 真正要问的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多