A表数据 name  id state
        张三   1  null
        李四   2  null
        王五   3  null
        周六   4  null
B表数据 id    
        2
        4
        5
希望能通过update得到如下结果
        name  id state
        张三   1   no
        李四   2   yes
        王五   3   no
        周六   4   yes就是说如果A表中的ID再B表中能够找到,那么state为yes,如果不能则为no

解决方案 »

  1.   

    select a.name,a.id,decode(b.id,null,'no','yes')from a,b
    where a.id = b.id(+)
      

  2.   

    select a.name, a.id, nvl2(b.id, 'yes', 'no') as state
      from a, b
     where a.id = b.id(+)
      

  3.   


    SQL> select * from a;
     
    NAME                         ID STATE
    -------------------- ---------- --------------------
    张三                          1 
    李四                          2 
    王五                          3 
    周六                          4 
     
    SQL> select * from b;
     
            ID
    ----------
             2
             4
             5
     
    SQL> 
    SQL> select a.name,a.id,decode(b.id,null,'no','yes') state
      2  from a,b
      3  where a.id = b.id(+)
      4  order by a.id;
     
    NAME                         ID STATE
    -------------------- ---------- -----
    张三                          1 no
    李四                          2 yes
    王五                          3 no
    周六                          4 yes
     
    SQL> 
    SQL> select a.name, a.id, nvl2(b.id, 'yes', 'no') as state
      2  from a, b
      3  where a.id = b.id(+)
      4  order by a.id;
     
    NAME                         ID STATE
    -------------------- ---------- -----
    张三                          1 no
    李四                          2 yes
    王五                          3 no
    周六                          4 yes
      

  4.   

    想问一下,如果B表中的数据大于A表,是否查询出来的数据也会大于A表的数据条数呢
      

  5.   

    select a.name, a.id, decode(b.id, null, 'no', 'yes')
      from a, (select distinct id from b) b
     where a.id = b.id(+)
      

  6.   

    select count(*) from (
    select d.sp_id,d.serv_id,decode(c.serv_id,null,'2','1')
    from 
    (select distinct serv_id,local_net_id,billing_cycle_id from ods.acct_item_owe@HNODSDB) c,
    sp_tj subpartition(m200906_d0730) d
    where d.serv_id=c.serv_id(+)
    and c.local_net_id='730'
    )其中c对应的是我开始说的B表,也就是配置表,总量有300多万条
        d对应的是我开始说的A表,也就是数据表,总量11万条
      

  7.   

    select count(*) from ( 
    select d.sp_id,d.serv_id,decode(c.serv_id,null,'2','1') 
    from 
    (select distinct serv_id from ods.acct_item_owe@HNODSDB) c, 
    sp_tj subpartition(m200906_d0730) d 
    where d.serv_id=c.serv_id(+) 
    and c.local_net_id='730' 
      

  8.   

    楼上的兄弟。。(select distinct serv_id from ods.acct_item_owe@HNODSDB) c, 
    会报错的
    因为后面用到了and c.local_net_id='730' 
      

  9.   

    select count(*) from ( 
    select d.sp_id,d.serv_id,decode(c.serv_id,null,'2','1') 
    from 
    (select distinct serv_id from ods.acct_item_owe@HNODSDB  where local_net_id='730') c, 
    sp_tj subpartition(m200906_d0730) d 
    where d.serv_id=c.serv_id(+)) 
      

  10.   

    如下是Standard SQL:update A a set state = case 
    when exists(select b.id from B b where b.id=a.id) then 'yes'
    else 'no' end
    /
      

  11.   

    update A a set state = case 
    when exists(select b.id from B b where b.id=a.id) then 'yes'
    else 'no' end
     
    yz394777014 回答的这个应该是OK的
      

  12.   

    oracle中涉及两个表的跟新确实很麻烦。之所以更新很多,因为,例如
    update a.filed1 = 
    select b.filed2 form b where a.id = b.id这时a表中,所有记录都会更新。
    所以要加上where ,再限定一次条件。update a.filed1 = 
    select b.filed2 form b where a.id = b.id
    where ( exist slect 'X' from 
    a, b where  a.id = b.id)以前碰到过类似的问题。
      

  13.   

    oracle中涉及两个表的跟新确实很麻烦。 之所以更新很多,因为,例如 
    update a.filed1 = 
    select b.filed2 form b where a.id = b.id 这时a表中,所有记录都会更新。 
    所以要加上where ,再限定一次条件。 update a.filed1 = 
    select b.filed2 form b where a.id = b.id 
    where ( exist slect 'X' from 
    a, b where  a.id = b.id) 以前碰到过类似的问题。 
      

  14.   

    我也写了一个
    UPDATE A
       SET A.STATUS = DECODE((SELECT 'true'
                               FROM B
                              WHERE EXISTS (SELECT '1'
                                       FROM A C
                                      WHERE C.ID = B.ID
                                        AND C.ID = A.ID)),
                             'true',
                             'yes',
                             'no')
    结果是出来了,就是不知道,上面还在讨论的是什么问题,很好奇。学习中