A
A
AlexBoss2022-04-12 21:24:20
PostgreSQL
AlexBoss, 2022-04-12 21:24:20

How to do recursion with 'for' posgres?

Good afternoon. There is a table in which links are indicated, 'from_id' to 'to_id'. By 'from_id' I get all the ids it refers to, take them and do the same. How to write a query to get and bypass all 'to_id' until I hit 'to_id' = None in each chain and return all id's that I have bypassed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2022-04-12
@sergey-gornostaev

You can use a recursive query , but it's better to change the database schema under nested sets .

N
nApoBo3, 2022-04-13
@nApoBo3

A recursive query, but if there are many such queries, then the data scheme does not match the task. If not enough, it's better to do it in code, at a "higher" level.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question