日期:2014-05-17  浏览次数:20779 次

急...Oracle簡單修改語句
SQL code

--Table1
ID    NAME     AGE
1     小明     18
2     小張     20
3     小王     22

--Table2
ID    NAME     AGE
001   小明     
002   小王
003   小張     18


我現在想寫一條Oracle修改語句,根據2張表的NAME,把表1的AGE賦到表2的AGE里...菜鳥求解

------解决方案--------------------
SQL code

ID    NAME     AGE
1     小明     18
2     小張     20
3     小王     22

--Table2
ID    NAME     AGE
001   小明     
002   小王
003   小張     18

drop table t2;
create table t1(
id int,
name varchar2(20),
age int
);
create table t2(
id int,
name varchar2(20),
age int
);
insert into t1 values (1,'小明',18);
insert into t1 values (2,'小張',20);
insert into t1 values (3,'小王',22);
insert into t2 values (1,'小明',null);
insert into t2 values (2,'小王',null);
insert into t2 values (3,'小張',18);
commit;
update t2
   set t2.age = (select t1.age
                   from t1
                  where t1.name = t2.name
                    and rownum = 1)
 where exists (select 1 from t1 where t1.name = t2.name);
commit;
select * from t2;
 
                                     ID NAME                                                     AGE
--------------------------------------- -------------------- ---------------------------------------
                                      1 小明                                                      18
                                      2 小王                                                      22
                                      3 小張                                                      20