DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`getSpecialPrice`(vItemFk int(11),vClientFk int(11)) RETURNS decimal(10,2) NOT DETERMINISTIC READS SQL DATA BEGIN DECLARE price DECIMAL(10,2); SELECT rate3 INTO price FROM vn.priceFixed WHERE itemFk = vItemFk AND util.VN_CURDATE() BETWEEN started AND ended ORDER BY created DESC LIMIT 1; SELECT `value` INTO price FROM vn.specialPrice WHERE itemFk = vItemFk AND (clientFk = vClientFk OR clientFk IS NULL) AND started <= util.VN_CURDATE() AND (ended >= util.VN_CURDATE() OR ended IS NULL) ORDER BY id DESC LIMIT 1; RETURN price; END$$ DELIMITER ;