使用 MySQL 8.0 递归 CTE 查找直系后代的数量并传播到分层 table 中的父代
Find number of direct descendents and propagate to parents in hierarchical table with MySQL 8.0 recursive CTE
我试图在层次结构中的每个节点上累积直系后代的数量。对于没有后代的节点,计数应为 0。
总的来说,我想在多个上下文中应用不同类型的 counting/aggregation,在这些上下文中,层次结构没有像这样准确定义。因此我对递归解决方案很感兴趣。
考虑下面的代码。我如何 "invert" 这个查询,而不是计算深度,我计算后代并向上传播数字?
create table hierarchy (
name varchar(100),
location varchar(100),
parent_name varchar(100),
parent_location varchar(100)
) engine=InnoDB default charset=UTF8MB4;
truncate hierarchy;
insert into hierarchy values
('music', '/', NULL, NULL),
('classical', '/music', 'music', '/'),
('pop', '/music', 'music', '/'),
('rock', '/music', 'music', '/'),
('bach', '/music/classical', 'classical', '/music');
select * from hierarchy;
with recursive cte as
(
select name, location, parent_name, parent_location, 1 as depth
from hierarchy where parent_name is NULL and parent_location is NULL
union all
select a.name, a.location, a.parent_name, a.parent_location, depth + 1
from hierarchy as a inner join cte on a.parent_name = cte.name and a.parent_location = cte.location
)
select *
from cte;
输出为
name location parent_name parent_location depth
'music' '/' NULL NULL 1
'classical' '/music' 'music' '/' 2
'pop' '/music' 'music' '/' 2
'rock' '/music' 'music' '/' 2
'bach' '/music/classical' 'classical' '/music' 3
我最终感兴趣的是这个输出:
name location parent_name parent_location descendents
'music' '/' NULL NULL 3
'classical' '/music' 'music' '/' 1
'pop' '/music' 'music' '/' 0
'rock' '/music' 'music' '/' 0
'bach' '/music/classical' 'classical' '/music' 0
您似乎想要计算每个节点的直系后代的数量。如果是这样,我认为您不需要递归查询:一个简单的子查询就可以做到:
select
h.*,
(select count(*) from hierarchy h1 where h1.parent_name = h.name) descendants
from hierarchy h
| name | location | parent_name | parent_location | descendants |
| --------- | ---------------- | ----------- | --------------- | ----------- |
| music | / | | | 3 |
| classical | /music | music | / | 1 |
| pop | /music | music | / | 0 |
| rock | /music | music | / | 0 |
| bach | /music/classical | classical | /music | 0 |
我试图在层次结构中的每个节点上累积直系后代的数量。对于没有后代的节点,计数应为 0。
总的来说,我想在多个上下文中应用不同类型的 counting/aggregation,在这些上下文中,层次结构没有像这样准确定义。因此我对递归解决方案很感兴趣。
考虑下面的代码。我如何 "invert" 这个查询,而不是计算深度,我计算后代并向上传播数字?
create table hierarchy (
name varchar(100),
location varchar(100),
parent_name varchar(100),
parent_location varchar(100)
) engine=InnoDB default charset=UTF8MB4;
truncate hierarchy;
insert into hierarchy values
('music', '/', NULL, NULL),
('classical', '/music', 'music', '/'),
('pop', '/music', 'music', '/'),
('rock', '/music', 'music', '/'),
('bach', '/music/classical', 'classical', '/music');
select * from hierarchy;
with recursive cte as
(
select name, location, parent_name, parent_location, 1 as depth
from hierarchy where parent_name is NULL and parent_location is NULL
union all
select a.name, a.location, a.parent_name, a.parent_location, depth + 1
from hierarchy as a inner join cte on a.parent_name = cte.name and a.parent_location = cte.location
)
select *
from cte;
输出为
name location parent_name parent_location depth
'music' '/' NULL NULL 1
'classical' '/music' 'music' '/' 2
'pop' '/music' 'music' '/' 2
'rock' '/music' 'music' '/' 2
'bach' '/music/classical' 'classical' '/music' 3
我最终感兴趣的是这个输出:
name location parent_name parent_location descendents
'music' '/' NULL NULL 3
'classical' '/music' 'music' '/' 1
'pop' '/music' 'music' '/' 0
'rock' '/music' 'music' '/' 0
'bach' '/music/classical' 'classical' '/music' 0
您似乎想要计算每个节点的直系后代的数量。如果是这样,我认为您不需要递归查询:一个简单的子查询就可以做到:
select
h.*,
(select count(*) from hierarchy h1 where h1.parent_name = h.name) descendants
from hierarchy h
| name | location | parent_name | parent_location | descendants |
| --------- | ---------------- | ----------- | --------------- | ----------- |
| music | / | | | 3 |
| classical | /music | music | / | 1 |
| pop | /music | music | / | 0 |
| rock | /music | music | / | 0 |
| bach | /music/classical | classical | /music | 0 |