MySQL 查询查找所有子元素
MySQL query to find all child elements
我有两个表:
元素 :
id_element
name
elem1
CPV
elem2
CBO
elem3
CPV
育儿(引用元素)
id_element
id_element_elements
elem1
null
elem2
elem1
elem3
elem1
elem4
elem2
在这种情况下,是否有从单个元素中查找所有子元素的查询?
寻找类似的东西:
id_element
id_child
elem1
elem2
elem1
elem3
elem2
elem4
WITH RECURSIVE
cte AS ( SELECT *
FROM parenting
WHERE id_element_elements = 'elem1'
UNION ALL
SELECT parenting.*
FROM cte
JOIN parenting ON cte.id_element = parenting.id_element_elements )
SELECT id_element_elements id_element, id_element id_child
FROM cte
SELECT
p.id_element_elements,p.id_element
FROM elements e
right join parenting p
On e.id_element =p.id_element_elements
Where p.id_element_elements is not null
我有两个表:
元素 :
id_element | name |
---|---|
elem1 | CPV |
elem2 | CBO |
elem3 | CPV |
育儿(引用元素)
id_element | id_element_elements |
---|---|
elem1 | null |
elem2 | elem1 |
elem3 | elem1 |
elem4 | elem2 |
在这种情况下,是否有从单个元素中查找所有子元素的查询?
寻找类似的东西:
id_element | id_child |
---|---|
elem1 | elem2 |
elem1 | elem3 |
elem2 | elem4 |
WITH RECURSIVE
cte AS ( SELECT *
FROM parenting
WHERE id_element_elements = 'elem1'
UNION ALL
SELECT parenting.*
FROM cte
JOIN parenting ON cte.id_element = parenting.id_element_elements )
SELECT id_element_elements id_element, id_element id_child
FROM cte
SELECT
p.id_element_elements,p.id_element
FROM elements e
right join parenting p
On e.id_element =p.id_element_elements
Where p.id_element_elements is not null