我现在有两张表
A表
name  id  address 
1      2    四川
2      3    云南
3      4    贵州B表
name id address
11    2   成都
22    3   昆明现在我需要做的是,将B的address 前面加上 与其ID相对应的A表的address结果希望是这样
B表
name  id   address
11    2    四川成都
22    3    云南昆明请教一下,这条Sql该怎么写,还有就是如果要学习这一部分的知识,需要看什么资料,希望大家推荐一下。
(本来想弄200分的....提示发不了....)

解决方案 »

  1.   

    select bb.name, bb.id, aa.address||bb.address from A aa, B bb
    where aa.id = bb.id;
      

  2.   

    If you want to learn SQL or PL/SQL language, you can read PL/SQL best practice 
      

  3.   

    select bb.name, bb.id, aa.address||bb.address from A aa, B bb
    where aa.id = bb.id(+)--买一本入门书籍,然后多练习!
      

  4.   

    也许是我没有阐述清楚,我是想修改B表,将B表修改成 上面的样子。还有就是B表中,包含有许多和A表对应不上的数据,那一部分数据,希望在做了我上面说的操作后任然保留。
      

  5.   

    也许是我没有阐述清楚,我是想修改B表,将B表修改成 上面的样子。还有就是B表中,包含有许多和A表对应不上的数据,那一部分数据,希望在做了我上面说的操作后任然保留。
      

  6.   

    First
       create table test as select bb.name, bb.id, aa.address||bb.address address from
            A aa, B bb
            where aa.id (+)=bb.id;
    Second
        rename  B to test1;
    Third
        rename  test to B;
    Finally 
         drop table test1;
      

  7.   


    update B
    set address=aa..address||bb.address
    from A aa join B bb
    on aa.id = bb.id ;
      

  8.   

    是没说明白
    为保证“还有就是B表中,包含有许多和A表对应不上的数据,那一部分数据,希望在做了我上面说的操作后任然保留。”,a、b两表数据调换,a两条,b三条select b.name, b.id, 
           b.address || a.address    -- 基本的字符串连接
      from (select 1 name, 2 id, '四川' address from dual
            union
            select 2 name, 3 id, '云南' address from dual
            union
            select 3 name, 4 id, '贵州' address from dual
           ) b,             -- 用union将各条数据放在一起当做b表
           (select 11 name, 2 id, '成都' address from dual
            union
            select 22 name, 3 id, '昆明' address from dual
           ) a
     where b.id = a.id(+)     -- 前边表(b)的全部数据都保留看基础的,有课本的话看课本就可以
      

  9.   

    update B bb set bb.address=(select aa.address||bb.address from A aa, B bb
    where aa.id = bb.id(+))
    where exists(select  1 from A aa, B bb
    where aa.id = bb.id(+))
      

  10.   


    select b.name, b.id, b.address || a.address "address"
     from a,b
     where b.id = a.id(+)
      

  11.   


    接分,建议楼主看看 oracle的基础与提高吧.
      

  12.   

    你这样做的话,那原B表上万一建了索引啥的不就没了么?
    此法不妥,可用update啊
      

  13.   

    此楼的update语句应该就可以了。
    楼主说的“还有就是B表中,包含有许多和A表对应不上的数据,那一部分数据,希望在做了我上面说的操作后任然保留。”B表中许多和A表对应不上的(id不等??)数据压根就不会被update到,这个只会更新和A表id相对应的数据。
      

  14.   

    update B
    set address=aa..address||bb.address
    from A aa join B bb
    on aa.id = bb.id ;
      

  15.   

    楼上的写成sqlserver语法了,应该这样
    update b set address = (select address from a where a.id = b.id)||address
      

  16.   

    楼上会把b表,id关联不上的,修改成空。
    要是不是太大表可以如此:
    update b set address = (select a.address||b.address from a where a.id = b.idwhere id in (select id from a )
      

  17.   


    哈哈,这样行不update b set address = nvl((select address from a where a.id = b.id),'')||address
      

  18.   


    --教你个方法,顺便还能学习到一个知识点
    merge into B
         using A
            on(B.id = A.id)
     when matched then
       update set B.address = A.address||B.address;--方法二
    update B
       set B.address = B.address||(select A.address from A where A.id=B.id)
     where exists(select 1 from from A where A.id=B.id);--个人建议你使用方法一。
      

  19.   

    需要加个exists判断,只对能匹配到的数据进行update,避免不能匹配数据被置为null
      

  20.   


    --用这个吧,你还能学到新的知识点。
    merge into B
         using A
            on(B.id=A.id)
     when matched then 
      update set B.address = A.address  || B.address;