码迷,mamicode.com
首页 > 数据库 > 详细

MYSQL查询上级和下级

时间:2020-01-27 11:02:52      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:color   weight   style   soft   evel   col   span   str   ack   

读取 group_id为4的所有下级

SELECT ID.level, DATA.* FROM( 
SELECT 
@ids as _ids, 
( SELECT @ids := GROUP_CONCAT(group_id) 
FROM rbac_group 
WHERE FIND_IN_SET(parent_group_id, @ids) 
) as cids, 
@l := @l+1 as level 
FROM rbac_group, 
(SELECT @ids :=4, @l := 0 ) b 
WHERE @ids IS NOT NULL 
) id, rbac_group DATA 
WHERE FIND_IN_SET(DATA.group_id, ID._ids) 
ORDER BY level, group_id

读取 group_id为6的所有下级

SELECT ID.level, DATA.* FROM( 
SELECT 
@id as _id, 
( SELECT @id := parent_group_id 
FROM rbac_group 
WHERE group_id = @id 
) as _pid, 
@l := @l+1 as level 
FROM rbac_group, 
(SELECT @id := 6, @l := 0 ) b 
WHERE @id > 0 
) ID, rbac_group DATA 
WHERE ID._id = DATA.group_id 
ORDER BY level;

MYSQL查询上级和下级

标签:color   weight   style   soft   evel   col   span   str   ack   

原文地址:https://www.cnblogs.com/aipeli/p/12235494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!