两个表,update的问题

deyangwangyu 2005-12-02 01:08:42
有这样两个表:

tb1:
ID F1 F2
1 a
2 b
3 c

tb2:
ID F2
1 e
2 f

现在要将tb2中F2字段的内容更新到tb1中相应行的F2中,请问该怎么写(sql2000)?
...全文
141 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
brooks105 2005-12-02
  • 打赏
  • 举报
回复
create table tb1
(ID int, F1 varchar(10), F2 varchar(10))
insert tb1
select 1 ,'a',''
union all
select 2 ,'b',''
union all
select 3 ,'c',''

create table tb2
(ID int, F2 varchar(10))
insert tb2
select 1 ,'e'
union all
select 2 ,'f'


update tb1
set f2=tb2.f2 from tb2
where tb1.id=tb2.id

select * from tb1

ID F1 F2
----------- ---------- ----------
1 a e
2 b f
3 c

(所影响的行数为 3 行)
浩方软件HFWMS 2005-12-02
  • 打赏
  • 举报
回复
--建立测试环境
create table #tb1(ID int, F1 varchar(10), F2 varchar(10))
insert into #tb1

select 1, 'a',null union
select 2, 'b',null union
select 3 , 'c' ,null

create table #tb2(id int,F2 varchar(10))
insert into #tb2
select 1, 'e' union
select 2, 'f'
update #tb1 set F2=t2.F2 from #tb1 t1
inner join #tb2 t2 on t1.id=t2.id
select * from #tb1
drop table #tb1,#tb2
----------------------------
/*
ID F1 F2
1 a e
2 b f
3 c NULL
影响3行
*/

wangkenping 2005-12-02
  • 打赏
  • 举报
回复
1.update tb1 set f2=b.f2 from tb1 a inner join tb2 b on a.Id=b.Id
2.update tb2 set tb1.F2=tb2.f2 from tb1,tb2 where tb1.ID=tb2.ID
breeze19772002 2005-12-02
  • 打赏
  • 举报
回复
Update T1 set F2 = (Select f2 From T2 Where t2.id=t1.id)
forkzeng 2005-12-02
  • 打赏
  • 举报
回复
下面利用同步处理。
declare @f2 varchar(?)
declare @id int
select @f2=F2,@id=ID from tb2
update tb1(F2) vaules(@f2) where ID=@id;
ljleager 2005-12-02
  • 打赏
  • 举报
回复
update tb1 set F2 = (select F2 from tb2 where ID = tb1.ID)
feng1071 2005-12-02
  • 打赏
  • 举报
回复
update tb2
set tb2.F2=tb1.F2
from tb1,tb2
where tb1.ID=tb2.ID
feng1071 2005-12-02
  • 打赏
  • 举报
回复
update tb2
set tb1.F2=tb2.f2
from tb1,tb2
where tb1.ID=tb2.ID
z_hp 2005-12-02
  • 打赏
  • 举报
回复
请参考:
update tb1 set f2=b.f2 from tb1 a inner join tb2 b on a.Id=b.Id

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧