Изменения документа Рекурсивный запрос

Редактировал(а) Alexandr Fokin 2023/12/16 14:12

От версии 2.8
отредактировано Alexandr Fokin
на 2022/01/03 14:43
Изменить комментарий: К данной версии нет комментариев
К версии 2.7
отредактировано Alexandr Fokin
на 2021/10/25 11:26
Изменить комментарий: Updated parent field.

Сводка

Подробности

Свойства страницы
Содержимое
... ... @@ -1,36 +1,31 @@
1 1  {{code language="sql"}}
2 -WITH RECURSIVE tree (nm, id, level, pathstr)
3 -AS
2 +
3 +with recursive tree (nm, id, level, pathstr)
4 +as
4 4  (
5 5   --Первый элемент в выборке. Начало рекурсии
6 - SELECT
7 - nm,
8 - id,
9 - 0,
10 - cast('' as text)
11 - FROM tree_sample
12 - WHERE id_parent is null
7 + select
8 + nm, id, 0, cast('' as text)
9 + from tree_sample
10 + where id_parent is null
13 13  
14 - UNION ALL
12 + union all
15 15  
16 16   --Каждый последующий элемент рекурсии
17 - SELECT
18 - tree_sample.nm,
19 - tree_sample.id,
20 - t.level + 1,
21 - tree.pathstr + tree_sample.nm
22 - FROM tree_sample
23 - INNER JOIN tree
15 + select
16 + tree_sample.nm, tree_sample.id, t.level + 1, tree.pathstr + tree_sample.nm
17 + from tree_sample
18 + inner join tree
24 24   on tree.id = tree_sample.id_parent
25 25  )
26 26  
27 -SELECT
28 - id,
29 - space(level) + nm as nm
30 -FROM tree
31 -ORDER BY pathstr
22 + select
23 + id, space( level ) + nm as nm
24 + from tree
25 + order by pathstr
26 +
32 32  {{/code}}
33 33  
34 34  
35 -Рекурсивные SQL запросы
36 36  https://habr.com/ru/post/27439/
31 +