--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種做法...
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) 不知道是否可以,数据量大的话,这样的更新感觉会比较耗时
2种写法??? 来个update 来个merge么 哈哈
update (select t1.age age1,t2.age age2 from table1 t1,table2 t2 where t1.name=t2.name) set age2=age1;没有创建视图,意思和创建视图是一样的,t1.name上需要有唯一约束
Have a try.[code=SQL] update table2 set table2.age = (select table1.age from table1 where table1.name = table2.name);code]
代码如下: update table2 set table2.age = (select table1.age from table1 where table1.name = table2.name);
不会是老师布置的作业吧
可以对两张表进行连接,建一个视图,然后update视图。table1的name字段要有唯一约束或是主键
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)
不知道是否可以,数据量大的话,这样的更新感觉会比较耗时
来个update 来个merge么 哈哈
where t1.name=t2.name) set age2=age1;没有创建视图,意思和创建视图是一样的,t1.name上需要有唯一约束
update table2
set table2.age =
(select table1.age from table1 where table1.name = table2.name);code]
update table2
set table2.age =
(select table1.age from table1 where table1.name = table2.name);
哈哈,有意思,那你确认一下,你的table1 name有木有重复的呢
嘿嘿,如果table1名字没有重复的,那为什么会报这样的错误,俺是菜鸟,请大神给指点迷津