update..from 问题

来源:百度知道 编辑:UC知道 时间:2024/06/04 23:39:04
有table1和table2两个表
table1(不存在同名,和重复情况)
姓名 年龄
张三 50
李四 40
王五 22
table2
姓名 年龄
张三 ?
李四 ?
张三 ?
张三 ?
王五 ?
王五 ?
问题是,怎么能将table1表里的年龄更新到table2中,talbe2中有重复同名的情况

UPDATE table2 SET table2.年龄=table1.年龄 WHERE table2.姓名=table1.姓名;

不知道这样行不行,好久不用数据库,SQL语句忘光光了。。。

table1
id name age

table2
id name age

C++代码我不熟悉
我大致写下思路
update table2 A
set age=(select b.age from table1 b where A.name=B.name)