题解 | #商品交易(网易校招笔试真题)#
商品交易(网易校招笔试真题)
http://www.nowcoder.com/practice/f257dfc1b55e42e19eec004aa3cb4174
# 查找购买个数超过20,质量小于50的商品,按照商品id升序排序,如:
#思路:先将两个表关联,然后对商品id进行分组,做SUM聚合操作求出总购买个数,在having中限制购买个数超过20,然后WHERE中排除质量小于50的商品数量,按标准SQL写法GROUP BY里有的字段才可以写入到SELECT中,所以答案如下
SELECT g.id,g.name,g.weight,SUM(t.count) total
FROM goods g JOIN trans t ON g.id = t.goods_id
WHERE g.weight < 50
GROUP BY t.goods_id,g.name,g.weight
HAVING SUM(t.count) > 20
ORDER BY g.id