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 (
20589, 20590, 20669, 20597, 20595, 20593,
20594, 20693, 20596, 20672, 20670,
20671, 20614, 20615, 20616, 20673,
20617, 20618, 20600, 20715, 20602,
20716, 20694, 20717
)
AND cscart_product_prices.lower_limit = 1
AND cscart_product_prices.usergroup_id IN (0, 1)
GROUP BY
cscart_product_prices.product_id