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

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 )
我想在一条语句上完成,我的水平菜鸟,实现不了.谢谢高手指点

...全文
76 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
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
去试试看吧

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧