SELECT goods_id AS id, NAME, weight , SUM(COUNT) total FROM ( SELECT goods_id, NAME,weight,COUNT FROM trans t LEFT JOIN goods g ON g.id=t.goods_id)a GROUP BY id HAVING weight<50 AND total>20 ORDER BY id ASC;