1.函數
MySQL 有很多內置的函數,例如函數 ASCII(s) 返回字符串 s 的第一個字符的 ASCII 碼。其實MySQL還可以自建函數,方便管理者使用。
2.函數與存儲過程的區別&優缺點
https://blog.csdn.net/riemann_/article/details/100831369
3.如何使用
- workbench
1.配置
因爲安全考慮,MySQL默認是不允許創建函數的(https://my.oschina.net/u/2331760/blog/854529),解決辦法是進入mysql客戶端輸入下面的一行指令。
mysql> SET GLOBAL log_bin_trust_function_creators = 1;
2.創建
3.編輯
CREATE DEFINER=`root`@`localhost` FUNCTION `get_tree_2`(treeid int) RETURNS char(100) CHARSET utf8 COLLATE utf8_bin
BEGIN
declare parent int default treeid;
declare treename char(100) default '';
set @flag = 0;
set @result = '';
mywhile:begin
while parent !=0 or parent != null do
select count(structuretree.treeName), structuretree.treeName, structuretree.parent into @flag, treename, parent from structuretree where structuretree.treeID = parent;
if @flag < 1 then
leave mywhile;
end if;
set @result = concat(@result,'/', treename);
end while;
end;
RETURN @result;
END
4.提交
5.使用
select suchen_server_items.get_tree_2(66);