题解 | #商品交易(网易校招笔试真题)#
商品交易(网易校招笔试真题)
http://www.nowcoder.com/practice/f257dfc1b55e42e19eec004aa3cb4174
根据题意要做两件事:
1.对trans根据good_id将count求和
2.筛选weight<50和total大于20的商品
这里懒得用子查询,直接内连接。注意这里的聚合函数不能用WHERE语句
SELECT a.id,a.name,a.weight,SUM(b.count) AS total
FROM goods AS a,trans AS b
WHERE a.id = b.goods_id
AND a.weight < 50
GROUP BY b.goods_id
HAVING SUM(b.count) > 20
ORDER BY a.id;