UPDATE sql for DB2中的INNER JOIN

Ham*_*ish 13 sql db2

有没有办法在DB2的update语句中使用连接?

谷歌真的让我失望了

这大致是我想要实现的目标(......除了明显的工作......)

update file1 inner join file2                                 
       on substr(file1.firstfield,10,20) = substr(file2.anotherfield,1,10)                                                                    
set file1.firstfield = ( 'BIT OF TEXT' concat file2.something )                                                                             
where file1.firstfield like 'BLAH%'                             
Run Code Online (Sandbox Code Playgroud)

干杯

Ian*_*vde 8

您没有说明您要定位的平台.但是,将表作为文件引用,使我相信您不是在Linux,UNIX或Windows(LUW)上运行DB2.

但是,如果您使用的 DB2 LUW,请参阅MERGE语句:

对于您的示例语句,这将写为:

merge into file1 a
   using (select anotherfield, something from file2) b
   on substr(a.firstfield,10,20) = substr(b.anotherfield,1,10)
when matched and a.firstfield like 'BLAH%'
   then update set a.firstfield = 'BIT OF TEXT' || b.something;
Run Code Online (Sandbox Code Playgroud)

请注意:对于DB2,SUBSTR函数的第三个参数是要返回的字节数,而不是结束位置.因此,SUBSTR(a.firstfield,10,20)返回CHAR(20).但是,SUBSTR(b.anotherfield,1,10)返回CHAR(10).我不确定这是否是故意的,但可能会影响你的比较.


Don*_*nie 7

加入update语句是非标准的,并非所有供应商都支持.您尝试做的事情可以通过子选择完成:

update
  file1
set
  firstfield = (select 'stuff' concat something from file2 where substr(file1.field1, 10, 20) = substr(file2.xxx,1,10) )
where
  file1.foo like 'BLAH%'
Run Code Online (Sandbox Code Playgroud)


小智 6

试试这个,然后告诉我结果:

UPDATE File1 AS B                          
SET    b.campo1 = (SELECT DISTINCT A.campo1
                   FROM  File2 A           
                   INNER JOIN File1      
                   ON A.campo2 = File1.campo2 
                   AND A.campo2 = B.campo2) 
Run Code Online (Sandbox Code Playgroud)

  • @Linger True,但多年后搜索者仍然可以找到答案.如果答案有效,则始终是相关的. (3认同)

Jus*_*tin 5

这是我刚刚开始工作的一个很好的例子:

update cac c
set ga_meth_id = (
    select cim.ga_meth_id 
    from cci ci, ccim cim 
    where ci.cus_id_key_n = cim.cus_id_key_n
    and ci.cus_set_c = cim.cus_set_c
    and ci.cus_set_c = c.cus_set_c
    and ci.cps_key_n = c.cps_key_n
)
where exists (
    select 1  
    from cci ci2, ccim cim2 
    where ci2.cus_id_key_n = cim2.cus_id_key_n
    and ci2.cus_set_c = cim2.cus_set_c
    and ci2.cus_set_c = c.cus_set_c
    and ci2.cps_key_n = c.cps_key_n
)
Run Code Online (Sandbox Code Playgroud)


Rob*_*ujo 5

更新答案/sf/answers/292896621/

如果你想要多列,可以这样实现:

update file1
set
  (firstfield, secondfield) = (
        select 'stuff' concat 'something from file2', 
               'some secondfield value' 
        from file2
        where substr(file1.field1, 10, 20) = substr(file2.xxx,1,10) )
where
  file1.foo like 'BLAH%'
Run Code Online (Sandbox Code Playgroud)

来源:http : //www.dbforums.com/db2/1615011-sql-update-using-join-subquery.html#post6257307


小智 5

只是为了仅更新符合条件的行,并避免更新其他行中的空值:

update table_one set field_1 = 'ACTIVE' where exists 
(select 1 from table_two where table_one.customer = table_two.customer);
Run Code Online (Sandbox Code Playgroud)

它适用于 DB2/AIX64 9.7.8