【问题标题】:SQL UPDATE SELECT with WHERESQL UPDATE SELECT 与 WHERE
【发布时间】:2018-01-26 20:16:22
【问题描述】:

我想UPDATE 表中的一列,但在每一行中必须有另一个值依赖于另一行的WHERE。

这就是表格的样子。

业务单元GUID |类名 |默认GUID

    5        | PriceList | 349FDAFD34M
    5        | Footer1   | 987IOXG376L
    5        | Header1   | 12WQX954MIO
    7        | PriceList | NULL
    7        | Footer1   | NULL
    7        | Header1   | NULL

结果应该是这样的。

业务单元GUID |类名 |默认GUID

    5        | PriceList | 349FDAFD34M
    5        | Footer1   | 987IOXG376L
    5        | Header1   | 12WQX954MIO
    7        | PriceList | 349FDAFD34M
    7        | Footer1   | 987IOXG376L
    7        | Header1   | 12WQX954MIO

但是这个显示的查询不起作用,因为它返回很多行,所以它不精确。

update cSC_BusinessUnit
set defaultguid =
    (
    select defaultguid
    from cSC_BusinessUnit 
    where BusinessUnitGUID = 5
    )
where BusinessUnitGUID = 7

【问题讨论】:

    标签: sql sql-server select where


    【解决方案1】:

    您还需要检查 ClassName:

    update b1
    set b1.defaultguid =
        (
        select b2.defaultguid
        from cSC_BusinessUnit b2
        where b2.BusinessUnitGUID = 5
            AND b2.ClassName = b1.ClassName
        )
    from cSC_BusinessUnit b1
    where b1.BusinessUnitGUID = 7
    

    【讨论】:

      【解决方案2】:

      也可以使用以下简短版本

      UPDATE  b1
      SET     b1.defaultguid = b2.defaultguid
      FROM    cSC_BusinessUnit b1
              JOIN
              cSC_BusinessUnit b2
                  ON b1.ClassName = b2.ClassName
                      And b2.BusinessUnitGUID = 5
      WHERE   b1.BusinessUnitGUID = 7
      

      【讨论】:

        【解决方案3】:

        这能解决您的问题吗?

        update cSC_BusinessUnit t1
        set defaultguid =
            (
            select defaultguid
            from cSC_BusinessUnit t2
            where t2.BusinessUnitGUID = 5
            and t1.classname = t2.classname
            )
        where BusinessUnitGUID = 7
        

        【讨论】:

        • 他使用的是 SQL Server 而不是 Oracle
        【解决方案4】:

        您也可以像这样动态设置所有 NULL 列,只要您只出现一次 ClassName NOT NULL

        update A
        set DefaultGUID =
            (
                select B.DefaultGUID
                from cSC_BusinessUnit B
                where B.ClassName = A.ClassName
                And B.DefaultGUID IS NOT NULL
            )
        from cSC_BusinessUnit A
        

        【讨论】:

        • 如果ClassName出现更多NOT NULL可能会导致错误
        • 确实,此查询仅在 ClassName 仅出现一次具有 DefaultGUID NOT NULL 的上下文中有效
        【解决方案5】:

        你可以试试

        update cSC_BusinessUnit a
        set a.defaultguid =
            (
            select defaultguid
            from cSC_BusinessUnit b
            where b.BusinessUnitGUID = 5 and b.ClassName = a.ClassName
            )
        where a.BusinessUnitGUID = 7
        

        【讨论】:

        • OP 使用的是 SQL-server/T-SQL,所以update cSC_BusinessUnit a 是错误的。有关 T-SQL 的正确语法,请参阅接受的答案。
        • 我用 pl/sql 来做,但我认为我的想法将用于这个问题。谢谢你
        【解决方案6】:
        update b1
        set b1.defaultguid =
            (
            select b2.defaultguid
            from cSC_BusinessUnit b2
            where b2.BusinessUnitGUID = 5
                AND b2.ClassName = b1.ClassName
            )
        from cSC_BusinessUnit b1
        where b1.BusinessUnitGUID = 7
        

        【讨论】:

          猜你喜欢
          • 2020-01-25
          • 1970-01-01
          • 2011-06-28
          • 1970-01-01
          • 2015-03-18
          • 2013-02-17
          • 1970-01-01
          • 2021-03-31
          • 2016-06-05
          相关资源
          最近更新 更多