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 ...