SELECT g.*, t1.total total FROM goods g INNER JOIN ( SELECT t.goods_id, SUM( t.count ) total FROM goods g LEFT JOIN trans t ON g.id = t.goods_id GROUP BY goods_id HAVING SUM( count ) > 20 ) AS t1 ON g.id = t1.goods_id WHERE g.weight < 50 ORDER BY g.id ASC 整体采用内连接和左连接的思路;首先生成一个经过分组后商品数量>20筛选后商品id和总数的临时表,然后与商品表用条件weight<50做内连接。