Revision 13e1138e
b/database/recursive_query_examples.sql | ||
---|---|---|
1 |
// This returns all subfolders (of any depth) of folder with id 3 |
|
2 |
with recursive subfolders(id) AS ( |
|
3 |
select id from folder where parent_id=3 |
|
4 |
UNION ALL |
|
5 |
select f.id from |
|
6 |
folder f, subfolders s |
|
7 |
where f.parent_id=s.id |
|
8 |
) |
|
9 |
select id from subfolders; |
|
10 |
|
|
11 |
// This returns the line of ancestors of folder with id 1140 |
|
12 |
with recursive parent(id, parent_id) AS ( |
|
13 |
select id, parent_id from folder where id=1140 |
|
14 |
UNION ALL |
|
15 |
select f.id, f.parent_id from |
|
16 |
folder f, parent p |
|
17 |
where p.parent_id=f.id |
|
18 |
) |
|
19 |
select id from parent; |
Also available in: Unified diff