By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
select version();
version() |
---|
8.0.27 |
CREATE TABLE table1(source VARCHAR(20), Binded VARCHAR(20));
INSERT INTO table1 VALUES
('Document0','Profile1'),
('Book1','Document1'),
('Document1','Book2'),
('Book2','Document2'),
('Profile1','Document2');
WITH RECURSIVE cte AS (
SELECT
source as s,
CAST(Binded AS CHAR(1024)) as b,
1 as c
FROM table1 WHERE Binded='Document2'
UNION ALL
SELECT table1.source, CONCAT(cte.s,'-',cte.b), c+1
FROM cte
LEFT JOIN table1 ON table1.Binded = cte.s
WHERE c<10 AND NOT cte.s IS NULL
)
select b
from cte
where cte.s is null and NOT b is null
group by b
;
b |
---|
Document0-Profile1-Document2 |
Book1-Document1-Book2-Document2 |