--TABLE1
ID NAME AGE
1 小張 18
2 小王 20
3 小明 22--TABLE2
ID NAME AGE
001 小王 18
002 小明 20
003 小明
004 小張 22
表結構應該是這樣的,TABLE1的NAME不會重複,而TABLE2的NAME會重複,要根據TABLE1和TABLE2的NAME把TABLE1的AGE賦到TABLE2對應的AGE上..最好有2種做法...

解决方案 »

  1.   

    update table2 t1 set age = (select age from table1 t2 where t2.name=t1.name) where age is null;太简单了没人回答啊...
      

  2.   

    謝謝,不過我想要2種做法,你這條語句爲什麽Where後面加age等於null呢,我想把表2的age都改了...
      

  3.   

    那是你的问题了,我给的只是个方法,你自己照着修改
    不会是老师布置的作业吧
    可以对两张表进行连接,建一个视图,然后update视图。table1的name字段要有唯一约束或是主键
      

  4.   

    update table2 tt
       set tt.age =  (select v.age
                       from (select t1.id,
                                    t1.name,
                                    (select t.age
                                       from table1 t
                                      where t.name = t1.name) age
                               from table2 t1) v
                      where v.id = tt.id)
    不知道是否可以,数据量大的话,这样的更新感觉会比较耗时
      

  5.   

    2种写法???
    来个update  来个merge么 哈哈 
      

  6.   

    update (select t1.age age1,t2.age age2 from table1 t1,table2 t2
      where t1.name=t2.name) set age2=age1;没有创建视图,意思和创建视图是一样的,t1.name上需要有唯一约束
      

  7.   

    Have a try.[code=SQL]
    update table2
       set table2.age =
           (select table1.age from table1 where table1.name = table2.name);code]
      

  8.   

    代码如下:
    update table2
      set table2.age =
      (select table1.age from table1 where table1.name = table2.name);
      

  9.   

    你的报错: ORA-01427: 單行子查詢返回多于一個行 
      

  10.   


    哈哈,有意思,那你确认一下,你的table1 name有木有重复的呢
      

  11.   

    没有重复...我想用2种做法看看效率问题,其实2种做法是我自己要求的,刚学Oracle,求大神解惑..
      

  12.   


    嘿嘿,如果table1名字没有重复的,那为什么会报这样的错误,俺是菜鸟,请大神给指点迷津