mysql query upper (lower) department

1. Use the ancestors list (ancestors) field:

1.1 preparation

CREATE TABLE `my_dept` (
  `dept_id` int NOT NULL AUTO_INCREMENT COMMENT 'id值',
  `parent_id` int DEFAULT NULL COMMENT '上级部门',
  `ancestors` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '祖级列表',
  `dept_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '名称',
  `sort` int DEFAULT NULL COMMENT '排序',
  `status` bit(1) NOT NULL COMMENT '状态',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`dept_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

insert image description here
There is an ancestor list field in the department table, which is used to describe all parent nodes.

1.2 Query subordinates

#查询部门ID为1的下级部门
SELECT * FROM `my_dept` WHERE FIND_IN_SET(1,ancestors)

insert image description here
We disassemble the statement, by the way, the FIND_IN_SET function.
FIND_IN_SET(str, strlist) is used to query whether the string str on the left is in the string list strlist on the right, and if it exists, it returns the position of the first occurrence, otherwise it returns 0.
Example:

insert image description here
insert image description here
insert image description here
We know that the number 0 or non-zero can be used as a filter condition when querying,
insert image description here
insert image description here
and then look at the statement we query the lower-level department
insert image description here

1.3 Query the superior:

insert image description here

SELECT * from my_dept
WHERE  FIND_IN_SET(dept_id,
	(SELECT CONCAT(ancestors,',',dept_id) FROM my_dept WHERE dept_id =8)
)	

2.with recursive as recursive query (Mysql8)

2.1 preparation

First add data in the table to deepen the level
insert image description here
mysql8 has added a recursive query method

Recursive writing:

WITH recursive 表名 AS ( 
	初始语句(非递归部分) 
	UNION ALL 
	递归部分语句
)
[ SELECT| INSERT | UPDATE | DELETE]

2.2 Query subordinates:

insert image description here

-- 递归查询下级部门
WITH RECURSIVE tree_cte as
 (
				 select dept_id, dept_name, parent_id, 0 as lv
				 from my_dept
				 where dept_id = 1

				 UNION ALL

				 select t.dept_id, t.dept_name, t.parent_id, lv + 1
				 from my_dept t
				 join tree_cte tcte on t.parent_id = tcte.dept_id 
 )
 SELECT * from tree_cte ORDER BY lv 

Statement disassembly:
insert image description here
insert image description here
insert image description here
insert image description here

2.3 Query the superior

insert image description here

#递归查询上级部门
WITH RECURSIVE tree_cte as
 (
				 select dept_id, dept_name, parent_id, 0 as lv
				 from my_dept
				 where dept_id = 8

				 UNION ALL

				 select t.dept_id, t.dept_name, t.parent_id, lv + 1
				 from my_dept t
				 join tree_cte tcte on t.dept_id = tcte.parent_id and t.dept_id !=0
 )
 SELECT * from tree_cte ORDER BY lv desc

3. Local variable recursive query

Example of local variable usage:
insert image description here
query example:
insert image description here

select t3.* from (
	select t1.dept_id,t1.parent_id,t1.dept_name,
	if(find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', dept_id), 0) as ischild
	from 
	(
			 select dept_id,parent_id,dept_name from my_dept t order by parent_id, dept_id		
	) t1,
	(select @pids := 5) t2
 ) t3 where ischild != 0 or dept_id = 5
 

SQL dismantling:

insert image description here
insert image description here
insert image description here
insert image description here
Query the superior department:

insert image description here

Add ID

select t4.* from (
	select @pids as dept_id,
	(SELECT @pids := parent_id FROM my_dept WHERE dept_id = @pids) as pid
	from 
	(
			 select dept_id,parent_id,dept_name from my_dept t 
	) t1,
	(select @pids := 9) t2 
 ) t3
 JOIN
 my_dept t4 
 on t4.dept_id = t3.dept_id 

insert image description here

 select t4.* from (
	select @pids as dept_id,
	(SELECT @pids := parent_id FROM my_dept WHERE dept_id = @pids) as pid,
	@lv := @lv+1 as lv
	from 
	(
			 select dept_id,parent_id,dept_name from my_dept t 
	) t1,
	(select @pids := 9, @lv := 0) t2 
 ) t3
 JOIN
 my_dept t4 
 on t4.dept_id = t3.dept_id ORDER BY lv desc

Guess you like

Origin blog.csdn.net/worilb/article/details/124646612