当前位置:网站首页>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结果:
边栏推荐
- Niuke brush questions - Sword finger offer (phase II)
- C language explanation series - understanding of functions (1) library functions, user-defined functions
- 简述redis特点及其应用场景
- Pytorch (V) -- pytorch advanced training skills
- H1--HDMI接口测试应用2022-07-15
- Epidemic period plus midlife crisis - three months wandering at the crossroads
- [swift bug] Xcode prompt error running playground: failed to prepare for communication with playground
- Dynamic memory management
- The topic pub instruction of ros2 appears: failed to populate field: 'vector3' object has no attribute 'x:1' error
- Notes and Thoughts on the red dust of the sky (III) as long as the conditions are sufficient, the results will come naturally
猜你喜欢

一次 MySQL 误操作导致的事故,「高可用」都不好使了

Deploy storageclass trample record

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

PXE remote installation and kickstart unattended installation technical documents

SVG, canvas, drawing line segments and filling polygon, rectangle, curve drawing and filling

7. Texture mapping

Why does MySQL index use b+ tree?

6、重心坐标插值和图形渲染管线

【达人专栏】还不会用Apache Dolphinscheduler吗,大佬用时一个月写出的最全入门教学【二】

Cadence learning path (VIII) PCB placement components
随机推荐
52832dongle installation
7. Texture mapping
Redis源碼與設計剖析 -- 7.快速列錶
Xssgame games (XSS learning) level1-15
6. Barycentric coordinate interpolation and graphics rendering pipeline
Custom events in components
Mysql database foundation
简述redis特点及其应用场景
web调用接口上传图片到七牛云
Deploy metersphere
Chapter 1 Overview - Section 1 - 1.2 overview of the Internet
R语言使用DALEX包对h2o包构建的机器学习模型进行解释分析:总结及实战
3dMax先蒙皮刷权重,再附加合并
C EventHandler observer mode
Kubernetes technology and Architecture (VI)
Li Nan, CTO of Yunqian Technology: technology and technical people, coevolution with digitalization
20. Valid brackets
"The six programming languages I want most!"
单点登录-认证服务器与客户端的session过期时间如何统一
The topic pub instruction of ros2 appears: failed to populate field: 'vector3' object has no attribute 'x:1' error




