这样的sql语句应该怎样写

quansui quansui 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
create table a
(
id int,
je1 money,
je2 money,
je3 money,
je4 money,
je5 money,
je6 money,
je7 money,
primary key(id)
)
create table b
(
id int,
aid int,
iType varchar(10),
je money,
primary key(id)
)
--
insert a select 1,0,0,0,0,0,0,0
union select 2,0,0,0,0,0,0,0
union select 3,0,0,0,0,0,0,0
union select 4,0,0,0,0,0,0,0
union select 5,0,0,0,0,0,0,0
union select 6,0,0,0,0,0,0,0
union select 7,0,0,0,0,0,0,0
insert b select 1,1,'type1',10
union select 2,1,'type2',10.5
union select 3,1,'type2',1
union select 4,2,'type1',2
union select 5,1,'type5',12
union select 6,3,'type1',13
union select 7,1,'type2',14
union select 8,2,'type3',15
union select 9,3,'type5',16
union select 10,4,'type6',17
union select 11,5,'type7',18
union select 12,5,'type2',19
union select 13,1,'type3',19
union select 14,7,'type4',10
union select 15,1,'type5',11
union select 16,6,'type6',111

现在想写一条更新表a的语句,要求把表b里的je合计按照表a的id和表b的aid对应,按表b的类型不同填到表a的不同字段里
例如表b的type1就填到表a的je1里
想了半天也没有想出来简单的

update a
set je1=(select sum(je) from b where a.id=b.aid and b.iType='type1')
,je2=(select sum(je) from b where a.id=b.aid and b.iType='type2')
,je3=(select sum(je) from b where a.id=b.aid and b.iType='type3')
,je4=(select sum(je) from b where a.id=b.aid and b.iType='type4')
,je5=(select sum(je) from b where a.id=b.aid and b.iType='type5')
,je6=(select sum(je) from b where a.id=b.aid and b.iType='type6')
,je7=(select sum(je) from b where a.id=b.aid and b.iType='type7')
select * from a
感觉这么写太笨了,请高手看看
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
quansui
疑难问题 22,298 成员 121,727 内容