Solution to find all child rows for a given parent row in MySQL

Solution to find all child rows for a given parent row in MySQL

Preface

Note: The test database version is MySQL 8.0

If you need to create tables and enter data under user scott, you can refer to:

Scott creates tables and enters data SQL script

1. Demand

Find all employees who work for JONES directly and indirectly (i.e., subordinates of JONES' subordinates).

The list of employees under JONES is as follows:

±------±-----+
| ename | lvl |
±------±-----+
| JONES | 1 |
| SCOTT | 2 |
| FORD | 2 |
| ADAMS | 3 |
| SMITH | 3 |
±------±-----+

2. Solution

Being able to move to the absolute top and bottom of a number is very useful.

No special formatting is required for this solution. The goal is simply to return all employees who are under employee JONES, which includes JONES himself.

This type of query demonstrates the usefulness of recursive SQL extensions such as Oracle's connect by and SQL Server/DB 2/MySQL 8.0's with clause.

with recursive emp2(ename,empno,lvl) as
(
SELECT ename,empno,1 lvl
 from emp 
 where ename = 'JONES'
union ALL
select e1.ename,e1.empno,lvl + 1
 from emp e1,emp2 e2
 where e1.mgr = e2.empno
)
select ename,lvl from emp2

Testing Log:

mysql> with recursive emp2(ename,empno,lvl) as
 -> (
 -> SELECT ename,empno,1 lvl
 -> from emp
 -> where ename = 'JONES'
 -> union ALL
 -> select e1.ename,e1.empno,lvl + 1
 -> from emp e1,emp2 e2
 -> where e1.mgr = e2.empno
 -> )
 -> select ename,lvl from emp2;
+-------+------+
| ename | lvl |
+-------+------+
| JONES | 1 |
| SCOTT | 2 |
| FORD | 2 |
| ADAMS | 3 |
| SMITH | 3 |
+-------+------+
5 rows in set (0.01 sec)

Summarize

This is the end of this article about finding all child rows for a given parent row in MySQL. For more information about finding all child rows for a given parent row in MySQL, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Docker installation and deployment example on Linux

>>:  Some tips on deep optimization to improve website access speed

Recommend

Vue-cli creates a project and analyzes the project structure

Table of contents 1. Enter a directory and create...

MySQL's conceptual understanding of various locks

Optimistic Locking Optimistic locking is mostly i...

MYSQL database GTID realizes master-slave replication (super convenient)

1. Add Maria source vi /etc/yum.repos.d/MariaDB.r...

Two ways to declare private variables in JavaScript

Preface JavaScript is not like other languages ​​...

Writing daily automatic backup of MySQL database using mysqldump in Centos7

1. Requirements: Database backup is particularly ...

Detailed explanation of the data responsiveness principle of Vue

This article is mainly for those who do not under...

Sample code for nginx to achieve dynamic and static separation

1. Simple configuration of nginx's dynamic an...

Example of using CASE WHEN in MySQL sorting

Preface In a previous project, the CASE WHEN sort...

Detailed explanation of how to dynamically set the browser title in Vue

Table of contents nonsense text The first router/...

CocosCreator Getting Started Tutorial: Making Your First Game with TS

Table of contents premise TypeScript vs JavaScrip...