select id, name, weight, sum(count) as total from ( select t1.id, t1.name, t1.weight, t2.count from goods t1 join trans t2 on t1.id = t2.goods_id where weight < 50 ) as select_data group by id having total > 20 order by id