SELECT `r`.`ball`
FROM (
(SELECT `ball`, 0 AS `order`
FROM `ratings`
WHERE `type_id` = :priceItemsId
AND `type_name` = 'price_items')
UNION (SELECT `r`.`ball` AS `ball`, 1 AS `order`
FROM `price_items` AS `i`
JOIN `ratings` AS `r`
ON `i`.`id` = :priceItemsId
AND `r`.`type_id` = `i`.`price_id`
AND `r`.`type_name` = `price`)
) AS `r`
ORDER BY `r`.`order`
LIMIT 1