有一张表A,属性id,name,extend,其中extend字段为空,一张表C,属性id ,extend,c.extend不为空,
其中表C.id是表A.id子集。现在需要使用SQL实现这样的功能
如果A.id = C.id 那么 a.extend = c.extend 否则 a.extend = 0 ?

解决方案 »

  1.   

    select a.id,a.name,a.extend
    from a,c
    where a.id=c.id and a.extend = c.extend
    union all
    select a.id,a.name,a.extend
    from a,c
    where a.extend = 0 and not exists(select 1
    from c where a.id=c.id)
      

  2.   

    select A.id,nvl(A.extend,0) from A,C where A.id = C.id(+)
      

  3.   


    不好意思,我没有说明白,a.extend = c.extend 否则 a.extend = 0 是复制的意思,就是把c.extend赋值给a.extend.
      

  4.   

    select a.id,a.name,nvl2(a.extend,0)
    from  a,c
    where a.id=c.id
      

  5.   

    不是查询,可能是我没说明白,说白了就是修改A.extend
      

  6.   

    update a set extend=(
      select nvl(max(extend),0) from c where id=a.id
      and rownum<2)
      

  7.   


    请问这个能实现a.id 在c.id没有的情况下a.extend要用0填写??好像不行吧
      

  8.   

    update a 
    set extent =
    (select nvl(extent,0) from c where id = a.id)
      

  9.   

    update A set extend = 0 where A.id not in (select id from C)
      

  10.   


    update a set extend=(select nvl(extend,0) from c where a.id=c.id)
      

  11.   


    SQL> select * from c;ID NA     EXTEND
    -- -- ----------
    1  a          11
    2  b          22
    3  c          33SQL> select * from a;ID NA     EXTEND
    -- -- ----------
    1  a
    2  b
    3  c
    4  d
    5  eSQL> update a set extend=(
      2    select nvl(max(extend),0) from c where id=a.id
      3    and rownum<2)
      4  /5 rows updated.SQL> select * from a;ID NA     EXTEND
    -- -- ----------
    1  a          11
    2  b          22
    3  c          33
    4  d           0
    5  e           0