米
心情
所有
图集
登录
搜索
原创
MySQL根据父级id(pid)递归查找子集、MySQL自定义函数查看删除等(父子关系查找)
米醋儿
发布于:2023-01-07
  最近在写文档系统,做删除的时候发现需要递归进行删除,以前没有注意这个事情,今天就详细研究一下,主要用到了MySQL的自定义函数(因为我用的5.7版本)。 ## 定义函数 函数名 get_child_list 表名 zhe_document_relation 主键 id 父级 pid 推荐 ``` create function get_child_list(in_id varchar(10)) returns varchar(1000) READS SQL DATA begin declare ids varchar(1000) default ''; declare tempids varchar(1000) DEFAULT ''; set tempids = in_id; while tempids is not null do set ids = CONCAT_WS(',',ids,tempids); select GROUP_CONCAT(id) into tempids from zhe_document_relation where FIND_IN_SET(pid,tempids)>0; end while; return ids; end ``` 不推荐 ``` delimiter $$ drop function if exists get_child_list$$ create function get_child_list(in_id varchar(10)) returns varchar(1000) begin declare ids varchar(1000) default ''; declare tempids varchar(1000); set tempids = in_id; while tempids is not null do set ids = CONCAT_WS(',',ids,tempids); select GROUP_CONCAT(id) into tempids from zhe_document_relation where FIND_IN_SET(pid,tempids)>0; end while; return ids; end $$ delimiter ; ``` ## 递归查找 ``` select * from zhe_document_relation where FIND_IN_SET(id,get_child_list('1172')) ``` ### 查看自定义函数 ``` // 查看自定义的所有函数 show function status // 可以根据名称查看所有自定义的函数,支持模糊查询 show function status like '名字'; show function status like '%me'; show function status like "ym_date" // 查看函数的创建语句 show create function '函数名字'; show create function ym_date //使用函数 SELECT ym_date(established_date) from py_etl_private_fund_base_info_2_1 ``` ## 删除自定义函数 ``` // 删除对应函数 drop function '函数名'; drop function ym_date2; ``` 鸣谢: 递归查找相关资料 https://www.cnblogs.com/guohu/p/14990788.html 自定义函数 https://blog.csdn.net/Lq_520/article/details/108128580
注:原创不易,转载请注明出处(
https://micuu.com/new/3183.html
),本站所有资源来源于网络收集,如有侵权请联系QQ245557979进行清除。
最后修改与 2023-08-14
上一篇:
vue-editormd在数据更新后重新渲染
下一篇:
今天的汤一股精子味儿
留言反馈
请先登录
问题反馈渠道,如有软件无法下载或者其他问题可反馈。【由于某种原因,目前留言不展示】
用户需要登录后才能留言反馈
立即留言
珍藏视频
10分钟高效燃脂
30天高效瘦脸操
5分钟缓解颈椎操
友人
微博
全民K歌
唱吧
今日头条
悠悠网
科技小锅盖
彼岸桌面
阮一峰
laravel社区
V2ex
掘金
更多>