Mysql example of querying all lower-level multi-level sub-departments based on a certain department ID

Mysql example of querying all lower-level multi-level sub-departments based on a certain department ID

Simulation tables and data scripts

Copy the following SQL statement to generate a table called sys_dept and insert some hierarchical data. Just copy and execute it.

DROP TABLE IF EXISTS `sys_dept`;
CREATE TABLE `sys_dept` (
 `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Department number',
 `p_id` int(11) DEFAULT NULL COMMENT 'Parent department number',
 `title` varchar(255) DEFAULT NULL COMMENT 'Parent department name',
 `is_open` int(11) DEFAULT NULL COMMENT 'Whether to expand (0-expand, 1-not expand)',
 `address` varchar(255) DEFAULT NULL COMMENT 'Department address',
 `create_time` datetime DEFAULT NULL COMMENT 'Creation time',
 `remark` varchar(255) DEFAULT NULL COMMENT 'Remarks',
 PRIMARY KEY (`id`) USING BTREE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO sys_dept (p_id,title,is_open,address,create_time,remark) VALUES 
(0,'General Manager's Office',1,'Shenzhen','2019-04-10 14:06:32.000','Big Boss')
,(1,'Sales Department',0,'Wuhan','2019-04-10 14:06:32.000','Programmer Loser')
,(1,'Operation Department',0,'Wuhan','2019-04-10 14:06:32.000','None')
,(1,'Production Department',0,'Wuhan','2019-04-10 14:06:32.000','None')
,(2,'Sales Department 1',0,'Wuhan','2019-04-10 14:06:32.000','Sales Department 1')
,(2,'Sales Department 2',0,'Wuhan','2019-04-10 14:06:32.000','Sales Department 2')
,(2,'Sales Department 3',0,'Guangzhou','2019-04-10 14:06:32.000','Sales Department 3')
,(2,'Sales Department 4',0,'Guangzhou','2019-04-10 14:06:32.000','Sales Department 4')
,(2,'Sales Department 5',0,'Guangzhou','2019-04-10 14:06:32.000','Sales Department 5')
,(3,'Operation Department 1',0,'Wuhan','2019-04-10 14:06:32.000','Operation Department 1')
,(3,'Operation Department 2',0,'Wuhan','2019-04-10 14:06:32.000','Operation Department 2')
,(3,'Operation Department 3',0,'Wuhan','2019-04-10 14:06:32.000','Operation Department 3')
,(3,'Operation Department 4',0,'Wuhan','2019-04-10 14:06:32.000','Operation Department 4')
,(3,'Operation Department 5',0,'Wuhan','2019-04-10 14:06:32.000','Operation Department 5')
,(4,'Production Department 1',1,'Shenzhen','2019-11-23 09:50:23.000','Production Department 1')
,(4,'Production Department 2',1,'Shenzhen','2019-11-23 09:50:23.000','Production Department 2')
,(4,'Production Department 3',1,'Shenzhen','2019-11-23 09:50:23.000','Production Department 3')
,(5,'Sales of one group',1,'Shenzhen','2019-11-23 09:50:23.000','Sales of one group')
,(5,'Sales Department 1, Group 2',1,'Shenzhen','2019-11-23 09:50:23.000','Sales Department 1, Group 2')
,(5,'Sales of Unit 1 and 3',1,'Shenzhen','2019-11-23 09:50:23.000','Sales of Unit 1 and 3')
,(6,'Sales Department 2 Group 1',1,'Shenzhen','2019-11-23 09:50:23.000','Sales Department 2 Group 1')
,(6,'Sales Department 2 Group 2',1,'Shenzhen','2019-11-23 09:50:23.000','Sales Department 2 Group 2')
,(17,'Production Department 3 Group 1',1,'Shenzhen','2019-11-23 09:50:23.000','Production Department 3 Group 1')
,(17,'Production Department 3 Group 2',1,'Shenzhen','2019-11-23 09:50:23.000','Production Department 3 Group 2')
,(17,'Production Department 3 Group 3',1,'Shenzhen','2019-11-23 09:50:23.000','Production Department 3 Group 3')
;

Query all sub-departments by department ID

select
 id,title
from
 (
 select
 t1.id,t1.title,
 if(find_in_set(p_id, @pids) > 0,
 @pids := concat(@pids, ',', id),
 0) as ischild
 from
 (
 select
  id,
  p_id,
  title
 from
  ssmdemo.sys_dept t
 order by
  p_id,
  id ) t1,
 (
  select @pids := 17) t2 ) t3
where
 ischild != 0

In @pids := 17, 17 is the department ID to be queried.

This is the end of this article about the example of MySQL querying all lower-level multi-layer sub-departments based on a certain department ID. For more relevant MySQL ID query all lower-level multi-layer sub-departments content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The difference and advantages and disadvantages of Mysql primary key UUID and auto-increment primary key
  • The unreasonable MaxIdleConns of MySQL will cause short connections
  • Detailed explanation of seven methods of returning auto-increment ID after inserting data in MySQL
  • Detailed steps to use IDEA to configure Tomcat and connect to MySQL database (JDBC)
  • MYSQL database GTID realizes master-slave replication (super convenient)
  • Solution to running out of MySQL's auto-increment ID (primary key)
  • JDBC-idea import mysql to connect java jar package (mac)
  • In-depth analysis of why MySQL does not recommend using uuid or snowflake id as primary key
  • How does MySQL implement ACID transactions?
  • Problems and solutions for IDEA connecting to MySQL
  • MySQL chooses the appropriate data type for id

<<:  Detailed explanation of HTML table tags (suitable for beginners)

>>:  JavaScript to achieve click image flip effect

Recommend

How to quickly paginate MySQL data volumes of tens of millions

Preface In backend development, in order to preve...

vue+tp5 realizes simple login function

This article example shares the specific code of ...

How to run py files directly in linux

1. First create the file (cd to the directory whe...

Implementation code of using select to select elements in Vue+Openlayer

Effect picture: Implementation code: <template...

An analysis of div+float, a very important concept in website design

In website construction, you will always encounter...

How to use physics engine joints in CocosCreator

Table of contents mousejoint mouse joint distance...

Defining the minimum height of the inline element span

The span tag is often used when making HTML web p...

Linux Network System Introduction

Table of contents Network Information Modify the ...

Detailed explanation of HTML form elements (Part 2)

HTML Input Attributes The value attribute The val...

Vue implements simple notepad function

This article example shares the specific code of ...

Detailed explanation of storage engine in MySQL

MySQL storage engine overview What is a storage e...