日期:2014-05-18  浏览次数:20742 次

关于两表的更新问题
如题:将A表中的不重复的数据提取出来,再将提取出来的数据插入到表B中,若B表中存在重复的则将B表中的数量字段增加1.两表中的识别码为NO.
谢谢了

------解决方案--------------------
能否舉例說明下要求,這樣更容易理解題意些。
------解决方案--------------------
----先更新
update b set num = num + 1
from tableB as b
inner join table A as a on b.NO = a.NO

----再插入
insert into tableB
select a.* from tableA as a
left join tableB as b on a.NO = b.NO
where b.NO is null

------解决方案--------------------
----先更新
update b set num = num + 1
from tableB as b
inner join (
select no from tableA group by no ) as a on b.NO = a.NO

----再插入
insert into tableB(no,num)
select no,1 from (
select no from tableA group by no ) as a
left join tableB as b on a.NO = b.NO
where b.NO is null

------解决方案--------------------
--先插
insert into tableB
select distinct a.* from tableA as a
left join tableB as b on a.NO = b.NO
where b.NO is null
-- 再update
update tableB A set A.Qty = (select sum(B.Qty) from tableB B where B.no = A.no)
--再讀
select distinct * from tableB