mysql的遍历树形结构

递归遍历树结构
DELIMITER //
CREATE FUNCTION `getChildrenList`(rootId CHAR(100))
RETURNS VARCHAR(1000)

BEGIN
DECLARE sTemp VARCHAR(1000);
DECLARE sTempChd VARCHAR(1000);

SET sTemp = '';
SET sTempChd =CAST(rootId AS CHAR);
 
WHILE sTempChd IS NOT NULL DO
SET sTemp = CONCAT(sTemp,',',sTempChd);
# group_concat(id)拼接字符串赋值给 sTempChd 然后find_in_set 中 parent_id在sTempChd 查找有没
#有,是否>0,>0则继续否则结束
SELECT GROUP_CONCAT(id) INTO sTempChd FROM rbac_resource WHERE FIND_IN_SET(parent_id,sTempChd)>0;
END WHILE;
RETURN sTemp;
END
//
DELIMITER ;

查询语句:
SELECT * FROM rbac_resource WHERE FIND_IN_SET(id,getChildrenList('OAR')); 
SELECT getChildrenList('OAR');

注释:
1.DELIMITER mysql解释器的结束的符号为;如 DELIMITER // 意思就是把;改变为//
2.cast(字段, as 转化的类型) 转化类型
3.concat(s1,s2,s3…)字符的拼接(若参数有为空的 则返回null)
4.group_concat(【disinct】字段 【order by asc/desc】【separator ‘,’】)把查询到的结果用逗号拼接成字#符串
5.find_in_set(str.strlist) str在strlist查找有则返回第几个位置的数 否则返回null

create table rbac_resource (
id char (60),
name varchar (192),
parent_id char (60),
app_code varchar (60),
product_manager varchar (1536),
project_manager varchar (1536),
group_id char (60),
scope char (6),
valid smallint (6),
deploy_url varchar (765),
remark varchar (765)
);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘0000000117’,‘参数管理-OA’,‘OAR’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘0000007005’,‘供应商管理’,‘OAR0000000010’,NULL,NULL,NULL,‘root’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR’,‘OA资源’,’’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,‘http://192.168.211.131:8380/oa-web’,‘OA系统有关的资源’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000008’,‘行政管理’,‘OAR’,NULL,‘OAUA2829|OAUA1102|0000001057’,‘OAUA1326|OAUA0558’,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000009’,‘人力管理’,‘OAR’,NULL,‘OAUA2829|OAUA1102|0000001057’,‘OAUA0558|OAUA1285’,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000010’,‘财务管理’,‘OAR’,NULL,‘OAUA2829|OAUA1102|0000001057’,‘OAUA1326|OAUA0558’,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000011’,‘库存管理’,‘OAR0000000008’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000012’,‘晚餐发布管理’,‘OAR0000000008’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000013’,‘会议室管理’,‘OAR0000000008’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000014’,‘制度公告’,‘OAR0000000008’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000015’,‘考勤管理’,‘OAR0000000009’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000016’,‘个人考勤’,‘OAR0000000009’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000017’,‘银行账号管理’,‘OAR0000000010’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000018’,‘预算信息管理’,‘OAR0000000010’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000019’,‘借款信息’,‘OAR0000000010’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000020’,‘员工信息管理’,‘OAR0000000009’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000021’,‘审计管理’,‘OAR0000000010’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000022’,‘运营管理’,‘OAR0000000010’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAR0000000099’,‘预算填报管理’,‘OAR0000000010’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OARK000000023’,‘项目管理’,‘OAR’,NULL,‘OAUA2829|OAUA1102|0000001057’,‘OAUA1326|OAUA0558’,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OARK000000024’,‘项目发版管理’,‘OARK000000023’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OARM000031’,‘邮箱管理’,‘OAR0000000008’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘OAW20180703’,‘欢迎页’,‘OAR’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,’’);
insert into rbac_resource (id, name, parent_id, app_code, product_manager, project_manager, group_id, scope, valid, deploy_url, remark) values(‘YWOAR’,‘运维管理-OA系统’,‘OAR’,NULL,NULL,NULL,‘ROOT000001’,‘04’,‘1’,’’,‘OA系统’);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值