- SQL code
--Table1ID NAME AGE1 小明 182 小張 203 小王 22--Table2ID NAME AGE001 小明 002 小王003 小張 18
我現在想寫一條Oracle修改語句,根據2張表的NAME,把表1的AGE賦到表2的AGE里...菜鳥求解
------解决方案--------------------
- SQL code
ID NAME AGE1 小明 182 小張 203 小王 22--Table2ID NAME AGE001 小明 002 小王003 小張 18drop 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