DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`ticketSplitCounter`(vTicketFk INT) RETURNS varchar(15) CHARSET utf8mb3 COLLATE utf8mb3_general_ci NOT DETERMINISTIC READS SQL DATA BEGIN DECLARE vSplitCounter VARCHAR(15); SELECT CONCAT(printedStickers,'/',Total, IF(printedStickers = Total ,' LS','')) INTO vSplitCounter FROM ( SELECT count(sl.saleFk) as printedStickers, COUNT(*) as Total FROM sale s LEFT JOIN saleLabel sl ON sl.saleFk = s.id WHERE ticketFk = vTicketFk ) sub; RETURN IFNULL(vSplitCounter,'--'); END$$ DELIMITER ;