SQL | SELECT b.brand_id, b.brand_name, b.brand_logo, b.brand_desc, COUNT(*) AS goods_num, IF(b.brand_logo > '', '1', '0') AS tag FROM `shop`.`ecs_brand`AS b, `shop`.`ecs_goods` AS g WHERE g.brand_id = b.brand_id AND g.cat_id IN ('1','21','14','435','436','15','20','17','22','16','403','432','433','430','434','431','405','413','410','411','412','400','407','408','437','438','409','406','401','416','417','414','415','402','423','424','425','422','404','429','426','427','428','29','419','420','421','418','13','28','25','26','30','27','24','9','31','38','35','32','36','33','37','34','10','44','41','45','42','39','46','43','40','12','47','54','51','48','52','49','53','50') AND is_show = 1 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 GROUP BY b.brand_id HAVING goods_num > 0 ORDER BY tag DESC, b.sort_order ASC LIMIT 3 |