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

两表更新字段的有关问题

2012-01-13 
两表更新字段的问题?有A,B两表,A表中有Pid字段,B表中有ID,Pid字段,当A表中的Pid字段值与B表中的Pid字段值

两表更新字段的问题?
有A,B两表,A表中有Pid字段,B表中有ID,Pid字段,当A表中的Pid字段值与B表中的Pid字段值相同时,取B表中的ID值更新A表的Pid值。
比如:A.Pid   =   234,B.id   =   5,B.Pid   =   234,更新后A.Pid   =   5;   这个Update语名如何写?

[解决办法]
有A,B两表,A表中有Pid字段,B表中有ID,Pid字段,当A表中的Pid字段值与B表中的Pid字段值相同时,取B表中的ID值更新A表的Pid值。
比如:A.Pid = 234,B.id = 5,B.Pid = 234,更新后A.Pid = 5; 这个Update语名如何写?

update A
set id = B.id
from A,B
where A.pid = B.pid
[解决办法]
有A,B两表,A表中有Pid字段,B表中有ID,Pid字段,当A表中的Pid字段值与B表中的Pid字段值相同时,取B表中的ID值更新A表的Pid值。
比如:A.Pid = 234,B.id = 5,B.Pid = 234,更新后A.Pid = 5; 这个Update语名如何写?

--------------------
update A
set t1.Pid=t2.id
from A t1 join B t2 on t1.Pid=t2.Pid

[解决办法]
update a set a.pid=b.id
from table1 a,table2 b
where a.pid=b.pid
[解决办法]
update A
set t1.Pid=t2.id
from A t1 join B t2 on t1.Pid=t2.Pid

热点排行