27,582
社区成员




UPDATE t1
SET t1.身高 = t2.身高
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.姓名=t2.姓名
--> 测试数据:[Table1]
if object_id('[Table1]') is not null drop table [Table1]
create table [Table1]([姓名] varchar(4),[年龄] int,[性别] varchar(2))
insert [Table1]
select '张三',18,'男' union all
select '李四',19,'女'
--> 测试数据:[Table2]
if object_id('[Table2]') is not null drop table [Table2]
create table [Table2]([姓名] varchar(4),[身高] int)
insert [Table2]
select '张三',160 union all
select '李四',150
--给第一个表添加身高字段
alter table [Table1] add [身高] int
go
--更新第一个表的数据:
update [Table1]
set [Table1].[身高]=a.[身高] from [Table2] a
where a.姓名=[Table1].姓名
--验证:
select * from [Table1]
/*
姓名 年龄 性别 身高
张三 18 男 160
李四 19 女 150
*/
update tb1
set 身高=tb2.身高
from tb1 join tb2 on tb1.姓名=tb2.姓名