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

Implementation of interactive data between QT and javascript

1. Data flows from QT to JS 1. QT calls the JS fu...

How to solve the error "ERROR 1045 (28000)" when logging in to MySQL

Today, I logged into the server and prepared to m...

Windows Server 2016 Quick Start Guide to Deploy Remote Desktop Services

Now 2016 server supports multi-site https service...

HTML table_Powernode Java Academy

To draw a table in HTML, use the table tag tr me...

Native javascript+CSS to achieve the effect of carousel

This article uses javascript+CSS to implement the...

What is jQuery used for? jQuery is actually a js framework

Introduction to jQuery The jQuery library can be ...

How to implement second-level scheduled tasks with Linux Crontab Shell script

1. Write Shell script crontab.sh #!/bin/bash step...

Very practical Tomcat startup script implementation method

Preface There is a scenario where, for the sake o...

12 Javascript table controls (DataGrid) are sorted out

When the DataSource property of a DataGrid control...

How to view and set the mysql time zone

1. Check the database time zone show variables li...