2014-10-11 154 views
0

我试图写的MySQL函数返回的孩子的计数为一个节点,而我写这篇代码MySQL函数没有返回任何值

CREATE DEFINER=`root`@`localhost` FUNCTION `get_child_count`(`id` INT) RETURNS int(11) 
READS SQL DATA 
Begin 
declare temp int(11); 
SELECT count(*) into temp FROM tbl_photo_album where parent_id = id; 
return temp; 
End 

,每次我试图运行这个功能它配备了没有。

+0

您是否试过在函数外部执行查询来查看它是否有效? – Barranka 2014-10-11 06:55:30

回答

0

可能的替代:中select ... into... Instad,试试这个:

set temp = (select count(*) from tbl_photo_album where parent_id = id); 
0

试试这个。

DELIMITER $$ 
CREATE DEFINER=`root`@`localhost` FUNCTION `get_child_count`(`id` INT) RETURNS int(11) 
READS SQL DATA 
Begin 
declare temp int(11); 
SELECT count(*) into temp FROM tbl_photo_album where parent_id = id; 
return temp; 
END$$ 
DELIMITER ; 

select get_child_count(10); -- Pass Id for what you want result.