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

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

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

思路:

  1. 先对trans表中,根据goods_id分组,使用having过滤出购买数量总和大于20的数据。
  2. 连接goods表与上面的临时表,即可查询出所需数据
select g.*, t.total
from goods as g join (select goods_id, sum(count) as total
											from trans
											group by goods_id
											having total > 20) as t
on g.id = t.goods_id
where g.weight < 50
order by g.id

也可使用 with ... as ... 将步骤一作为一个临时表,思路是一样的

with t as (select goods_id, sum(count) as total
					 from trans
					 group by goods_id
					 having total > 20)
select g.*, t.total
from goods as g 
join t
on g.id = t.goods_id
where g.weight < 50
order by g.id
SQL练习 文章被收录于专栏

已完成牛客的SQL练习。接下来是算法的练习

全部评论

相关推荐

赏个offer求你了:友塔HR还专门加我告诉我初筛不通过😂
点赞 评论 收藏
分享
无敌虾孝子:喜欢爸爸还是喜欢妈妈
点赞 评论 收藏
分享
1 1 评论
分享
牛客网
牛客企业服务