题解 | #商品交易(网易校招笔试真题)#

商品交易(网易校招笔试真题)

https://www.nowcoder.com/practice/f257dfc1b55e42e19eec004aa3cb4174

select g.id,g.name,g.weight,a.total from goods as g
inner join (select t.goods_id,sum(t.count) as total from trans t group by t.goods_id ) as a 
on g.id=a.goods_id
where a.total>20 and g.weight<50
order by id

1.首先对trans表用商品id 进行分组求和 形成新表a,里面字段是商品id和total

!注意:一定要起别名,清楚要查询的数据,确保运行能查出来

(select t.goods_id,sum(t.count) as total from trans t group by t.goods_id ) as a

2.然后表商品goods g和新表a内连接 ,都含商品id

select g.id,g.name,g.weight,a.total from goods as g

inner join (select t.goods_id,sum(t.count) as total from trans t group by t.goods_id ) as a

on g.id=a.goods_id

3.最后限定条件,

where a.total>20 and g.weight<50

order by id

全部评论

相关推荐

CrazyBucket:我今天下午也做梦在招聘会上面试一家小厂,给自己气笑了
点赞 评论 收藏
分享
09-27 14:42
已编辑
浙江大学 Java
未来未临:把浙大放大加粗就行
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务