如何使用内部联接更新行

问题描述

我有一条选择语句,在其中找到两行,我想更新特定的列:

select * from murex.STPDLV_ENTRY_TABLE a 
inner join (select DLV_FLOW_XX,max(TS_TIME_LONG) AS MaxDateTime from murex.STPDLV_ENTRY_TABLE group by DLV_FLOW_XX) b 
on a.DLV_FLOW_XX = b.DLV_FLOW_XX
and a.TS_TIME_LONG=b.MaxDateTime
and a.DLV_FLOW_XX in (3741539,4044126,3741551)
and a.STP_UDF9 !='Posted';

我尝试过:

update STPDLV_ENTRY_TABLE a
set a.STP_UDF9 = 'Posted'
where
inner join (select DLV_FLOW_XX,3741551);

如何正确构造更新查询?

解决方案:

update (select * 
        from STPDLV_ENTRY_TABLE a 
        inner join (select DLV_FLOW_XX,max(TS_TIME_LONG) AS MaxDateTime 
                    from STPDLV_ENTRY_TABLE 
                    group by DLV_FLOW_XX) b 
        on a.DLV_FLOW_XX = b.DLV_FLOW_XX
        and a.TS_TIME_LONG = b.MaxDateTime
        and a.DLV_FLOW_XX in (3741539,3741551)
        and a.STP_UDF9 !='Posted') test
set test.STP_UDF9 = 'Posted';

解决方法

对我来说,merge似乎是一个更好/更简单的选择。

MERGE INTO stpdlv_entry_table a
     USING (  SELECT dlv_flow_xx,MAX (ts_time_long) AS maxdatetime
                FROM murex.stpdlv_entry_table
            GROUP BY dlv_flow_xx) b
        ON (    a.dlv_flow_xx = b.dlv_flow_xx
            AND a.ts_time_long = b.maxdatetime)
WHEN MATCHED
THEN
   UPDATE SET a.stp_udf9 = 'Posted'
           WHERE a.dlv_flow_xx IN (3741539,4044126,3741551);
,

只需将查询作为表格放置

update (select * 
        from STPDLV_ENTRY_TABLE a 
        inner join (select DLV_FLOW_XX,max(TS_TIME_LONG) AS MaxDateTime 
                    from STPDLV_ENTRY_TABLE 
                    group by DLV_FLOW_XX) b 
        on a.DLV_FLOW_XX = b.DLV_FLOW_XX
        and a.TS_TIME_LONG = b.MaxDateTime
        and a.DLV_FLOW_XX in (3741539,3741551)
        and a.STP_UDF9 !='Posted') test
set test.STP_UDF9 = 'Posted';

这是一个演示:

DEMO

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...