当前位置:网站首页>MySql语句查询某一级节点的所有子节点
MySql语句查询某一级节点的所有子节点
2022-07-23 05:37:00 【九离⠂】
MySql语句查询某一级节点的所有子节点
在日常项目中,我们总能用到树型结构的数据,我们用代码去进行查询是比较麻烦的,这里提供一种sql语句查询父节点和子节点的方法。
说明:只能当前节点查出所有子节点,不包含与当前节点平级的节点,且子节点是全部返回,并没有分层分级。
1、表结构----建表语句
CREATE TABLE `group` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `parent_id` bigint(20) NOT NULL, `subsystem_id` int(11) NOT NULL, `group_name` varchar(60) NOT NULL, `create_time` datetime NOT NULL, `description` varchar(256) DEFAULT NULL, `available` bit(1) NOT NULL DEFAULT b'1' COMMENT '0::false,1:true', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='组';
2、表结构----数据
数据说明:
举例:
平台级角色组的id为1,那他的子节点包含2,4,3
企业域级角色组id为2,那他的子节点包含3
3、SQL语句模板
select id from ( select t1.id, if(find_in_set(父级id字段名, @pids) > 0, @pids := concat(@pids, ',', 主键id字段名), -1) as ischild from ( select 主键id字段名,父级id字段名 from 表名 t order by 父级id字段名, 主键id字段名 ) t1, (select @pids := 需要查询的主键id) t2 ) t3 where ischild != -1
语句说明:
首先分析from后面的语句,根据parent_id和id 排序,并将要查询的菜单节点当做变量,from后面的结果为
接下来看if(express1,express2,express3)条件语句,if语句类似三目运算符,当exprss1成立时,执行express2,否则执行express3;
FIND_IN_SET(str,strlist),str 要查询的字符串,strlist 字段名 参数以”,”分隔 如 (1,2,6,8),查询字段(strlist)中包含(str)的结果,返回结果为null或记录
如果parent_id 在@pid中,则将@pid 里面再加上parent_id,按行依次执行
此时执行的sql:
SELECT t1.id,t1.group_name, IF ( find_in_set( parent_id, @pids ) > 0, @pids := concat( @pids, ',', id ), -1 ) AS ischild FROM ( SELECT id, parent_id,group_name FROM `group` t ORDER BY parent_id, id ) t1, ( SELECT @pids := 1 ) t2执行过程如下表所示:
4、查询举例
4.1 查询平台级角色组(id=1)下级组
SELECT id ,group_name FROM ( SELECT t1.id,t1.group_name, IF ( find_in_set( parent_id, @pids ) > 0, @pids := concat( @pids, ',', id ), -1 ) AS ischild FROM ( SELECT id, parent_id,group_name FROM `group` t ORDER BY parent_id, id ) t1, ( SELECT @pids := 1 ) t2 ) t3 WHERE ischild != -1结果:
4.2 查询企业级角色组(id=2)下级组
SELECT id ,group_name FROM ( SELECT t1.id,t1.group_name, IF ( find_in_set( parent_id, @pids ) > 0, @pids := concat( @pids, ',', id ), -1 ) AS ischild FROM ( SELECT id, parent_id,group_name FROM `group` t ORDER BY parent_id, id ) t1, ( SELECT @pids := 2 ) t2 ) t3 WHERE ischild != -1结果:
边栏推荐
- R language uses DALEX package to explain and analyze the machine learning model built by H2O package: summary and Practice
- 部署metersphere
- C语言n番战--共用体和枚举(八)
- NOTIFIER诺帝菲尔消防主机电源维修及日常维护
- Error reporting when installing opencv in Anaconda virtual environment
- LearnOpenGL - Introduction
- Li Nan, CTO of Yunqian Technology: technology and technical people, coevolution with digitalization
- Pyqt5 use qpainter to draw the coordinate axis and display the scatter diagram
- 6、重心坐标插值和图形渲染管线
- [unity] avpro uses stepping pits, and the editor mode uses video playback. The video cannot be played after packaging
猜你喜欢

Mysql database foundation
![[visual slam] orb slam: tracking and mapping recognizable features](/img/bb/d6a99bb1bff6bbe1f781e567cc1176.png)
[visual slam] orb slam: tracking and mapping recognizable features

pyqt5使用QPainter绘制坐标轴并显示散点图

H1 -- HDMI interface test application 2022-07-15

Redis source code and design analysis -- 12. Collection objects

Redis source code and design analysis -- 13. Ordered collection objects

mysql invalid conn排查

12 open source background management systems suitable for outsourcing projects

PMP practice once a day | don't get lost in the exam -7.22

Switch exchanges
随机推荐
Understand asp Net core - Cookie based authentication
Fundamentals of software testing - design method of test cases
R语言使用DALEX包对h2o包构建的机器学习模型进行解释分析:总结及实战
Script of Nacos current limiting query
疫情时期加中年危机——游荡在十字街口的三个月
mysql invalid conn排查
Redis源码与设计剖析 -- 12.集合对象
Switch exchanges
PyTorch(五)——PyTorch进阶训练技巧
资源池以及资源池化是什么意思?
Redis source code and design analysis -- 14. Database implementation
[unity daily bug] unity reports an unexpected character '‘
Deploy metersphere
Detailed explanation of structure
Visual studio 2022 interesting and powerful intelligent auxiliary coding
Mysql数据库基础
[swift bug] Xcode prompt error running playground: failed to prepare for communication with playground
排序
12 open source background management systems suitable for outsourcing projects
Redis source code and design analysis -- 5. Integer set




