2014-10-11 150 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.