关于主从表更新的有关问题
关于主从表更新的问题
我有两个表分别是A和B,A和B是一对多的关系,A表字段如下:yhbh(关键字段,唯一),ye(number)。B表字段如下:yhbh,sbbh(唯一),ye(number)。现在我想把A表中的ye字段的信息更新到B表中对应yhbh的其中一行记录中的ye字段中,得保证这两个表中的ye字段的内容求和时是一样的。这样的sql怎么写?
------解决方案--------------------
我有两个表分别是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 ta select '001', 1.12 union all select '002', 1.23 create table tb (yhbh char(5), sbbh char(5), ye decimal(5,2)) insert into tb select '001', '01', 0 union all select '001', '02', 0 union all select '002', '03', 0 union all select '002', '04', 0 update tb set tb.ye=ta.ye from tb inner join ta on tb.yhbh=ta.yhbh where tb.sbbh in (select min(sbbh) from tb group by yhbh) select * from tb yhbh sbbh ye ----- ----- -------- 001 01 1.12 001 02 0.00 002 03 1.23 002 04 0.00 (4 row(s) affected)