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 ('2','100','55','385','361','68','357','65','62','359','66','63','360','67','64','61','57','386','84','81','78','82','79','83','80','77','58','388','90','87','91','88','85','92','89','86','59','97','94','98','95','99','96','93','60','106','103','107','104','101','108','105','102','56','74','71','75','72','69','76','73','70') 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 |