数据表中增加一列,我是菜鸟,谢谢了!!!
现有两张表a1,a2.
表a1如下:
d e
00001 dd
00002 aa
00003 cc
00004 ee
表a2如下:
f e
123 dd
456 ee
789 cc
根据e列,把a2表中f列添加到a1表中:
d e f
00001 dd 123
00002 aa
00003 cc 789
00004 ee 456
------解决方案--------------------如果是查询则:
select a1.*,a2.f from a1 Left Join a2 on a1.e=a2.e
如果是改a1的表结构:
alter table a1 add f varchar(20)
update a1 set f=a2.f from a2 where a1.e=a2.e
------解决方案--------------------ALTER TABLE a1 ADD f VARCHAR(20)
GO
UPDATE A SET f = B.f
FROM a1 A, a2 B WHERE A.e = B.e
------解决方案--------------------SQL code
alter table a1 add f varchar(50) --增加f列
update a1 set f=a2.f from a2 where a1.e=a2.e --插入数据
------解决方案--------------------
SQL code
select a1.* , isnull(a2.f,'') f from a1 left join a2 on a1.e = a2.e
------解决方案--------------------
alter table a1 add f int
update a1 set f=a2.f from a2 where a1.e=a2.e
------解决方案--------------------
select a1.* , isnull(a2.f,'') f from a1 left join a2 on a1.e = a2.e