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

关于主从表更新的有关问题

2012-04-03 
关于主从表更新的问题我有两个表分别是A和B,A和B是一对多的关系,A表字段如下:yhbh(关键字段,唯一),ye(numb

关于主从表更新的问题
我有两个表分别是A和B,A和B是一对多的关系,A表字段如下:yhbh(关键字段,唯一),ye(number)。B表字段如下:yhbh,sbbh(唯一),ye(number)。现在我想把A表中的ye字段的信息更新到B表中对应yhbh的其中一行记录中的ye字段中,得保证这两个表中的ye字段的内容求和时是一样的。这样的sql怎么写?

[解决办法]

SQL code
create table ta(yhbh char(5), ye decimal(5,2))insert into taselect '001', 1.12 union allselect '002', 1.23create table tb(yhbh char(5), sbbh char(5), ye decimal(5,2))insert into tbselect '001', '01', 0 union allselect '001', '02', 0 union allselect '002', '03', 0 union allselect '002', '04', 0update tb set tb.ye=ta.yefrom tbinner join ta on tb.yhbh=ta.yhbhwhere tb.sbbh in(select min(sbbh) from tb group by yhbh)select * from tbyhbh  sbbh  ye----- ----- --------001   01    1.12001   02    0.00002   03    1.23002   04    0.00(4 row(s) affected) 

热点排行