PrefaceNote: 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. DemandFind 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. SolutionBeing 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) SummarizeThis 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
Fault description percona5.6, mysqldump full back...
1. Data flows from QT to JS 1. QT calls the JS fu...
Today, I logged into the server and prepared to m...
Now 2016 server supports multi-site https service...
To draw a table in HTML, use the table tag tr me...
This article uses javascript+CSS to implement the...
Introduction to jQuery The jQuery library can be ...
1. Write Shell script crontab.sh #!/bin/bash step...
Preface There is a scenario where, for the sake o...
Preface As a heavy user of front-end frameworks, ...
When the DataSource property of a DataGrid control...
This article shares the installation and configur...
Table of contents # Post-data preparation # SQL q...
1. Check the database time zone show variables li...
Preface In databases such as Oracle and SQL Serve...