| SQL | SELECT
b.brand_id,
b.brand_name,
b.brand_logo,
b.sort_order,
CASE WHEN b.brand_logo != '' THEN 1 ELSE 0 END AS tag
FROM `chinajicai_cn`.`ecs_brand` AS b
INNER JOIN (
-- 子查询先过滤有效商品,只返回唯一brand_id,大幅缩小JOIN数据集
SELECT DISTINCT g.brand_id
FROM `chinajicai_cn`.`ecs_goods` AS g
WHERE 1=1
g.cat_id IN ('2','58','678','658','664','659','660','661','662','665','666','667','669','668','670','671','672','673','674','675','676','677','679','680','681','682','683','684','685','686','687','688','689','690','691','861')
AND g.is_on_sale = 1
AND g.is_alone_sale = 1
AND g.is_delete = 0
) AS g ON g.brand_id = b.brand_id
WHERE b.is_show = 1
-- 排序改用原生字段计算,不依赖别名tag,可配合索引减少filesort
ORDER BY IF(b.brand_logo = '', 0, 1) DESC, b.sort_order ASC
|
| 错误 | You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'g.cat_id IN ('2','58','678','658','664','659','660','661','662','665','666','66' at line 13 |