代码之家  ›  专栏  ›  技术社区  ›  nearly_lunchtime

检查SQL CASE语句中是否存在

  •  2
  • nearly_lunchtime  · 技术社区  · 17 年前

    a b .目前:

    update a
    set a.field1 =
     case
     when exists (
       select b.field2
       from b
       where b.field2 = a.field2
     )
     then 'FOO'
     else 'BAR'
     end
    

    你没有跑步。你知道如何为DB2数据库做到这一点吗?

    编辑 :谢谢你的回答,我能做的就是

    update a set field1 = 'FOO' where field2 in (select field2 from b);
    
    update a set field1 = 'BAR' where field2 not in (select field2 from b);
    

    但我会把这个打开,以防有人能在顶部找到一个有效的代码版本。

    4 回复  |  直到 15 年前
        1
  •  6
  •   bluish dmajkic    15 年前

    update a
    set a.field1 =
     Coalesce( ( select 'FOO'
                 from b
                 where b.field2 = a.field2 ),
                          'BAR' )
    

    Coalesce()

        2
  •  3
  •   Lieven Keersmaekers    17 年前

    这在SQLServer中有效。也许DB2有类似的结构。

    update a SET field1 = 'BAR'
    from a
         left outer join b on b.field1 = a.field1
    where b.field1 is null;
    update a SET field1 = 'FOO'
    from a
         inner join b on b.field1 = a.field1
    

    当做

        3
  •  0
  •   Walter Mitty    17 年前

    你说“表中的另一列是否在一个集合中……”

    您的代码正在修改同一列,而不是另一列。

        4
  •  0
  •   C B dkretz    9 年前

    我不是SQL或DB2方面的专家,但也许您可以将这两个表连接起来,检查b.field1是否为空?

    update a
    set a.field1 = case when b.field1 is not null then 'FOO' else 'BAR' end
    from a full outer join b on a.field1 = b.field1