首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

更新表的某字段到关联表的另一字段的sql

2012-03-25 
求一个更新表的某字段到关联表的另一字段的sql表 a字段 a1,a2表 b字段 b1,a1,b2关联关系为 a.a1b.a1a.a1

求一个更新表的某字段到关联表的另一字段的sql
表 a 字段 a1,a2
表 b 字段 b1,a1,b2
关联关系为 a.a1=b.a1
a.a1是主键
现在要把a.a2都赋值成b.b2,求破。

[解决办法]
update a set a2=b.a1 from b where a.a1=b.b2
[解决办法]

SQL code
goif OBJECT_ID('a')is not nulldrop table  agocreate table a(id int,num int)goinsert aselect 1,2 union allselect 2,2 union allselect 3,2 union allselect 4,2goif OBJECT_ID('b')is not nulldrop table  bgocreate table b(id int,num int)goinsert bselect 1,1 union allselect 2,2 union allselect 3,3 union allselect 4,4update a set num=b.num from b where a.id=b.idselect * from a/*id    num1    12    23    34    4*/
[解决办法]
这样破?
update a set a2=b.b2 from b where a.a1=b.a1
也可以这样破
update a, b set a.a2=b.b2 where a.a1=b.a1
[解决办法]
探讨
表 a 字段 a1,a2
表 b 字段 b1,a1,b2
关联关系为 a.a1=b.a1
a.a1是主键
现在要把a.a2都赋值成b.b2,求破。

热点排行
Bad Request.