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 insert 10 million records into a MySQL database table in 88 seconds

The database I use is MySQL database version 5.7 ...

JavaScript to achieve product magnifying glass effect

This article shares the specific code of JavaScri...

How to automatically execute SQL statements when MySQL in Docker starts

When creating a MySQL container with Docker, some...

How to quickly insert 10 million records into MySQL

I heard that there is an interview question: How ...

A brief discussion on the mysql execution process and sequence

Table of contents 1:mysql execution process 1.1: ...

How to deploy MySQL master and slave in Docker

Download image Selecting a MySQL Image docker sea...

Vue realizes price calendar effect

This article example shares the specific code of ...

JavaScript and JQuery Framework Basics Tutorial

Table of contents 1. JS Object DOM –1, Function –...

Introduction and usage examples of ref and $refs in Vue

Preface In JavaScript, you need to use document.q...

The most complete package.json analysis

Table of contents 1. Overview 2. Name field 3. Ve...

js to achieve drag and drop sorting details

Table of contents 1. Introduction 2. Implementati...