SELECT *, ( IF ('2020-07-13 01:52:57' >= sp.`from` AND '2020-07-13 01:52:57' <= sp.`to`, 1, 0) + IF (sp.`id_country` = 3, 2, 0) + IF (sp.`id_group` = 1, 4, 0) + IF (sp.`id_currency` = 1, 8, 0) + IF (sp.`id_shop` = 4, 16, 0) + IF (sp.`id_customer` = 0, 32, 0)) AS `score` FROM `ps_specific_price` sp INNER JOIN `ps_specific_price_rule` spr ON `sp`.`id_specific_price_rule` = `spr`.`id_specific_price_rule` AND spr.check_compatibility = 1 WHERE sp.`id_product` IN (0, 6235) AND sp.`id_product_attribute` IN (0, 7643) AND sp.`id_shop` IN (0, 4) AND sp.`id_currency` IN (0, 1) AND sp.`id_country` IN (0, 3) AND sp.`id_group` IN (0, 1) AND sp.`id_customer` IN (0, 0) AND ( (sp.`from` = '0000-00-00 00:00:00' OR '2020-07-13 01:52:57' >= sp.`from`) AND (sp.`to` = '0000-00-00 00:00:00' OR '2020-07-13 01:52:57' <= sp.`to`) ) AND sp.id_cart IN (0, 0) AND sp.`from_quantity` <= 1 ORDER BY sp.`id_product_attribute` DESC, sp.`from_quantity` DESC, sp.`id_specific_price_rule` ASC, `score` DESC