我有两个表分别是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)