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 ('3','383','363','113','141','138','142','139','136','143','140','112','135','132','133','134','109','116','117','114','118','115','110','122','119','123','120','124','121','379','381','382','380','111','128','125','129','126','130','127','131') 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 |