知识点
- 首先把两个表连接,使用group by进行分组各类返回一行。
- 筛选条件使用having进行筛选最后对id进行排序。
代码
select g.id, name, weight, sum(count) as total
from trans as t
join goods as g
on t.goods_id = g.id
group by g.id
having total> 20 and weight < 50
order by g.id
select g.id, name, weight, sum(count) as total
from trans as t
join goods as g
on t.goods_id = g.id
group by g.id
having total> 20 and weight < 50
order by g.id