问一个表合并的问题,谢谢大家!

autobar 2006-02-12 06:16:44
TABLE bproduct
ProdID pnumber
4501 0
4502 0

TABLE yh_list
in_yhid in_num
4501 17
4501 10
4502 11
4502 12

怎样将 yh_list的数据按照In_yhid合并后插入TABLE bproduct ?
(合并我是这样写的select sum(in_num) from yh_list group by in_yhid )
我想在一条语句上完成,我的水平菜鸟,实现不了.谢谢高手指点

...全文
58 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
filebat 2006-02-12
if object_id('bproduct') is not null
drop table bproduct
go
create table bproduct (ProdID int, pnumber int)
insert bproduct select 4501, 0
union all select 4502, 0

if object_id('yh_list') is not null
drop table yh_list
go
create table yh_list(in_yhid int , in_num int)
insert yh_list select 4501, 17
union all select 4501, 10
union all select 4502, 11
union all select 4502, 12
go
update bproduct
set pnumber=pnumber+num
from (
select in_yhid, num=sum(in_num)
from yh_list
group by in_yhid
)t
where t.in_yhid=bproduct.prodid

select * from bproduct
回复
ReViSion 2006-02-12
update bproduct set
pnumber = B.sum_num
from bproduct A inner join
(select in_yhid,sum(in_num) as sum_num from yh_list group by in_yhid) B on A.ProdID=B.in_yhid
回复
$扫地僧$ 2006-02-12
update bproduct set pnumber=(select sum(in_num) from yh_list where ProdID=b.in_yhid )
from bproduct b
回复
OracleRoob 2006-02-12
update bproduct set
pnumber = B.sum_num
from bproduct A inner join
(select in_yhid,sum(in_num) as sum_num from yh_list group by in_yhid) B on A.ProdID=B.in_yhid

回复
hongfof 2006-02-12
select bproduct.pnumber,yh_list.in_num,bproduct.ProdID from bproduct,yh_list
where bproduct.bproduct=yh_list.in_yhid
去试试看吧
回复
相关推荐
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2006-02-12 06:16
社区公告
暂无公告