虽然麻烦但是挺好理解
商品交易(网易校招笔试真题)
http://www.nowcoder.com/practice/f257dfc1b55e42e19eec004aa3cb4174
select g.id as id,g.name as name,g.weight as weight,tt.total as total
from goods as g
left join trans as t
on g.id = t.goods_id
left join (
select t.goods_id as id,sum(t.count) as total from
trans as t left join goods as g
on t.goods_id = g.id
where g.weight<50
group by t.goods_id
) as tt
on tt.id = t.goods_id
where tt.total>20
group by g.id
order by g.id asc
主要就是子查询where g.weight<50
select t.goods_id as id,sum(t.count) as total from
trans as t left join goods as g
on t.goods_id = g.id
where g.weight<50
group by t.goods_id
查出id和销售总量total,再用where筛选小于50的
结束子查询后在外围筛选中获取总数大于20的,也就是where tt.total>20