E
E
Eugene2019-02-15 09:06:48
PHP
Eugene, 2019-02-15 09:06:48

Recursion in SQL query POSTGRESQL?

There is a table with fields id id_parent
1 2
2 *
3 1
4 5
5 6
6 4
7 *
as output I need a list id:
1 -> 2-> *(null) : 1,2
But there is still a small problem:
4-> 5->6->4... (and this is where the eternal loop starts, how to check?) : 4,5,6
A solution that works, but goes into an eternal loop and is very heavy....
WITH RECURSIVE r AS(
SELECT id ,id_parent
FROM table
WHERE teil_sachnr = '1'
UNION ALL
SELECT table.id,table.id_parent
FROM w_teil
JOIN r ON table.id=r.id_parent
)
SELECT * FROM r;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2019-02-15
@le_baron

Actually, the eternal loop can be in the data itself and, accordingly, it would be necessary to limit either the recursive cte (in sql ms is maxrecursion) or expand the recursion into a loop and there similarly control the "endless loop" in one way or another (limiting the depth, breaking when parent is sent to level below.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question