SELECT
cscart_product_prices.product_id,
COALESCE(
cscart_master_products_storefront_min_price.price,
MIN(
IF(
cscart_product_prices.percentage_discount = 0,
cscart_product_prices.price,
cscart_product_prices.price - (
cscart_product_prices.price * cscart_product_prices.percentage_discount
)/ 100
)
)
) AS price
FROM
cscart_product_prices
LEFT JOIN cscart_master_products_storefront_min_price ON cscart_master_products_storefront_min_price.product_id = cscart_product_prices.product_id
AND cscart_master_products_storefront_min_price.storefront_id = 1
WHERE
cscart_product_prices.product_id IN (
21604, 21607, 21606, 21602, 21599, 21617,
21616, 21611, 21595, 21597, 21578,
21596, 21577, 21561, 21562, 21563,
21610, 21615, 21601, 21598, 21591,
21592, 21593, 21594
)
AND cscart_product_prices.lower_limit = 1
AND cscart_product_prices.usergroup_id IN (0, 1)
GROUP BY
cscart_product_prices.product_id