A
A
AlexBoss2022-04-21 10:44:58
PostgreSQL
AlexBoss, 2022-04-21 10:44:58

How to make a recursive query returning all passed elements?

There is a table with fields name, from_id, to_id. How to make a query at the input of which comes from_id and recursively go through the entire table and collect all to_id including for all child elements?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2022-04-21
@AlexBoss

with recursive q as (
    select * from tes where from_id = 1
     union all
    select tes.* from tes, q where tes.from_id = q.to_id
)
select * from q order by from_id, to_id

https://www.db-fiddle.com/f/4jyoMCicNSZpjMt4jFYoz5/4445

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question