sql - 使用条件更新单个 sql 查询中的多个列

标签 sql database oracle

我需要帮助来构建符合以下条件的 sql 查询:

oracle 数据库中有 3 列,我需要更新其中一列,检查第 1 列是否为空,然后更新它,不要触及其他两列,如果第 1 列不为空,则更新第 2 列,不要更新第 3 列,否则如果第 3 列为空,则更新第 3 列。

我可以构建的查询是:

 update temp set 
    flg1 = 
        case flg1
            when null then 'Y' else flg1 end,
    flg2 = 
        case flg2
            when null then 'Y' else flg2 end,
    flg3 = 
        case flg3
        when null then 'Y' else flg3 end,
where id = 132

我知道上面的查询与我想要的不同,需要帮助..

最佳答案

试试这个:

 update temp set 
        flg1 = 
            case when flag1 is null then 'Y' else flg1 end,
        flg2 = 
           case when flag1 is not null and flag2 is null then 'Y' else flg2 end,
        flg3 = 
            case when flag1 is not null and flag2 is not null and flag3 is null then 'Y' else flg3 end
where id = 132

关于sql - 使用条件更新单个 sql 查询中的多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34199754/

相关文章:

java - 如何构建具有多列的 CriteriaQuery 谓词 IN 子句?

oracle - 在 Oracle 中识别具有最大行数的表

sql - 从多个数据库获取数据

java - 从 SQL Server 成功检索数据后 JTable 为空

database - psycopg2 - 类型错误 : 'int' object is not subscriptable

c# - 如何使用左连接、聚合和 where 子句创建 Linq to Sql

database - 将 RDS Postgres 与 Heroku 一起使用有多安全?

java - 如何修复ORA-00911 : invalid character in Mybatis query

sql - 如何在 Oracle Select 中获取列中的邻近数字

MySQL在创建和插入数据时收到语法错误