myql中常用上级路径包含本身

mysql自定义函数获取上级的路径实例

sql 复制代码
CREATE  FUNCTION `querydept_path_name`(depetId VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
BEGIN
DECLARE sTempPath VARCHAR(255);
DECLARE sTempChd VARCHAR(255);
DECLARE sFullPath VARCHAR(255);
 
SET sTempChd = depetId;
SET sFullPath ='';
SET sTempPath ='';
 
WHILE (sTempChd <> '' ) DO
select ifnull((SELECT dept_name FROM dept_info  WHERE id = sTempChd),'') INTO sTempPath;
SET sFullPath = CONCAT(sFullPath,sTempPath,'/');
select ifnull((SELECT parent_id FROM dept_info  WHERE id = sTempChd),'')  INTO sTempChd;

END WHILE;
 
RETURN sFullPath;
END

函数创建失败错误信息

创建函数时报错信息

sql 复制代码
[Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)

解决办法

解决方法

  1. 在mysql数据库中执行以下语句 (临时生效,重启后失效)
sql 复制代码
set global log_bin_trust_function_creators=TRUE;

或者

sql 复制代码
set global log_bin_trust_function_creators=1;
  1. 在配置文件/etc/my.cnf的[mysqld]或者my-default.ini文件中配置
sql 复制代码
[mysqld]
log_bin_trust_function_creators=1

测试函数路径

默认路径为逆序

sql 复制代码
select querydept_path_name('10011')

路径正序

sql 复制代码
select REVERSE(querydept_path_name('10011'))
相关推荐
FungLeo10 小时前
FengCMS · PHP+MySQL 升级实录 · 旧后台备份三宗罪:漏空表、改写内容、按分号切 SQL 直接执行
sql·mysql·php·安全加固·fengcms
SA-D10 小时前
MySQL 性能分析与库存一致性:从 Buffer Pool、B+ 树到执行计划
数据库·mysql
djjjx.1 天前
Mysql数据库基础篇(八):详细讲解复合查询、多表查询、自连接、多行、多列子查询与合并查询
数据库·mysql
MSTcheng.1 天前
【MySQL】MySQL数据库——表的操作
数据库·mysql·adb
FungLeo1 天前
FengCMS · PHP+MySQL 升级实录 · 内容写入一致性:锁等待超时与跨模型子树删除竞态
开发语言·mysql·php·工程实践·fengcms
ZhengEnCi1 天前
覆盖索引是什么:用借书和外卖看懂回表这件事
sql·mysql·postgresql
鹤落晴春1 天前
MySQL日志梳理
运维·数据库·mysql
FungLeo1 天前
FengCMS · PHP+MySQL 升级实录 · MySQL DDL不能回滚:模型字段结构操作的失败续做
android·mysql·php·工程实践·fengcms
隔窗听雨眠1 天前
MySQL锁等待的定位方法与治理体系
mysql·锁机制·数据库运维
千寻技术帮1 天前
基于Spring Boot+Vue的农产品销售管理系统(源码+文档+数据库,附部署教程)
java·数据库·spring boot·后端·mysql·vue