【问题标题】:How to update only matching columns in db2如何仅更新 db2 中的匹配列
【发布时间】:2017-01-19 13:14:21
【问题描述】:

我正在尝试执行如下所示的更新查询:

update semester_workload tA
set tA.working = (SELECT DEPUTAT_WS11 from TD_DOZENTEN t1 where t1.Pruefernummer = tA.ID_Lecturer AND tA.ACADEMIC_SEMESTER = 'WS11')

但是 db2 会自动更新整个列,而不是只更新与“where”语句匹配的数据。

我怎样才能让它只更新我想要的数据?

【问题讨论】:

    标签: sql database db2 sql-update


    【解决方案1】:

    一种方法是将逻辑放在where

    update semester_workload tA
        set tA.working = (select DEPUTAT_WS11 
                          from TD_DOZENTEN t1
                          where t1.Pruefernummer = tA.ID_Lecturer AND tA.ACADEMIC_SEMESTER = 'WS11')
        where exists (select 1
                      from TD_DOZENTEN t1
                      where t1.Pruefernummer = tA.ID_Lecturer AND tA.ACADEMIC_SEMESTER = 'WS11'
                     );
    

    我认为 DB2 不支持UPDATE 中的JOINFROM

    【讨论】:

    • 是的,成功了,谢谢!我会尽快验证
    • 没有必要让“AND tA.ACADEMIC_SEMESTER = 'WS11'”进入子选择;)
    【解决方案2】:

    你可以的

    UPDATE semester_workload t0
    SET t0.working = 
    (
        SELECT t1.DEPUTAT_WS11 FROM TD_DOZENTEN t1 
        WHERE t1.Pruefernummer = t0.ID_Lecturer
    )
    where t0.ACADEMIC_SEMESTER = 'WS11' and exists 
    (
        SELECT * FROM TD_DOZENTEN t1 
        WHERE t1.Pruefernummer = t0.ID_Lecturer
    )
    

    【讨论】:

      猜你喜欢
      • 2012-07-22
      • 1970-01-01
      • 2014-05-04
      • 2020-05-23
      • 1970-01-01
      • 2015-12-04
      • 2020-10-08
      • 2020-11-27
      • 1970-01-01
      相关资源
      最近更新 更多