SELECT
products.product_id,
descr1.product as product,
companies.company as company_name,
products.kl_erp_inventory_id,
products.product_type,
products.parent_product_id,
products.master_product_offers_count,
products.master_product_id,
products.company_id,
products.kl_enable_qc,
products.kl_qc_amount
FROM
cscart_products as products
LEFT JOIN cscart_product_descriptions as descr1 ON descr1.product_id = products.product_id
AND descr1.lang_code = 'en'
LEFT JOIN cscart_companies AS companies ON companies.company_id = products.company_id
INNER JOIN cscart_products_categories as products_categories USE INDEX (
idx_products_categories_composite
) ON products_categories.product_id = products.product_id
INNER JOIN cscart_categories ON cscart_categories.category_id = products_categories.category_id
AND cscart_categories.status IN ('A', 'H')
AND cscart_categories.storefront_id IN (0, 1)
LEFT JOIN cscart_product_sales USE INDEX (
idx_product_sales_category_amount
) ON cscart_product_sales.product_id = products.product_id
AND cscart_product_sales.category_id = products_categories.category_id
WHERE
1
AND (
companies.status IN ('A')
OR products.company_id = 0
)
AND products.company_id IN (13, 0)
AND (
products.amount > 0
OR products.tracking = 'D'
)
AND products.status IN ('A')
AND (
(
1
AND products.product_id IN (10869)
)
AND (
companies.status IN ('A')
OR products.company_id = 0
)
)
AND products.master_product_status IN ('A')
AND (
1
AND products_categories.category_id IN (
277, 281, 290, 296, 458, 508, 620, 782,
1186, 2215, 2438, 2453, 2463, 2473,
2488, 2494, 2640, 2641, 2642, 2643,
2644, 2645, 2646, 2647, 2648, 2649,
2650, 2651, 2652, 2653, 2654, 2655,
2656, 2657, 2658, 2659, 2660, 2661,
2662, 2663, 2664, 2665, 2666, 2667,
2668, 2669, 2670, 2671, 2672, 2673,
2674, 2675, 2676, 2677, 2678, 2679,
2680, 2681, 2682, 2683, 2684, 2913,
2973, 2974, 3020, 3055, 3070, 3078,
3093, 3094, 3097, 3098, 3099, 3100,
3101, 3102, 3103, 3104, 3105, 3106,
3107, 3114, 3152, 3161, 3162, 3163,
3173
)
)
GROUP BY
products.product_id
ORDER BY
cscart_product_sales.amount desc,
products.product_id ASC
LIMIT
0, 1