Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

recursive query - PostgreSQL get parent categories from table

I have the table as like below.

CREATE TABLE my.categories (id bigint, parent_id bigint, name varchar(128));

INSERT INTO my.categories (id, parent_id, name) VALUES (1, null, 'LEVEL 1');
INSERT INTO my.categories (id, parent_id, name) VALUES (2, 1, 'LEVEL 2.1');
INSERT INTO my.categories (id, parent_id, name) VALUES (3, 1, 'LEVEL 2.2');
INSERT INTO my.categories (id, parent_id, name) VALUES (4, 2, 'LEVEL 3.1.1');
INSERT INTO my.categories (id, parent_id, name) VALUES (5, 2, 'LEVEL 3.1.2');
INSERT INTO my.categories (id, parent_id, name) VALUES (6, 3, 'LEVEL 3.2.1');

+----+-----------+---------------+
| id | parent_id | name          |
+----+-----------+---------------+
|  1 |      null |     'LEVEL 1' |
|  2 |         1 |   'LEVEL 2.1' |
|  3 |         1 |   'LEVEL 2.2' |
|  4 |         2 | 'LEVEL 3.1.1' |
|  5 |         2 | 'LEVEL 3.1.2' |
|  6 |         3 | 'LEVEL 3.2.1' |
+----+-----------+---------------+

I need to get all id's for parent categories.

WITH RECURSIVE tree(theId) AS (
  SELECT id
  FROM my.categories
  WHERE id = theId -- wrong here, because its not a procedure
  UNION ALL
  SELECT table1.id
  FROM my.categories AS table1
    JOIN tree AS parent ON theId = table1.parent_id
)
SELECT DISTINCT theId FROM tree WHERE theId = 6;

Example result with data but actually I need only id's.

+----+-----------+---------------+
| id | parent_id | name          |
+----+-----------+---------------+
|  1 |      null |     'LEVEL 1' |
|  3 |         1 |   'LEVEL 2.2' |
|  6 |         3 | 'LEVEL 3.2.1' |
+----+-----------+---------------+

Or like this:

+----+-----------+---------------+
| id | parent_id | name          |
+----+-----------+---------------+
|  3 |         1 |   'LEVEL 2.2' |
|  6 |         3 | 'LEVEL 3.2.1' |
+----+-----------+---------------+

The trouble is I'm not allowed to use procedures. This query should be used as sub-query for many other queries. And please dont look at name column it is irrelevant.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I get you, this is what you need.

First, with the folowing query you can get all the parent ids:

WITH RECURSIVE t(id, parentlist) AS (
  SELECT id, ARRAY[]::bigint[] FROM my.categories WHERE parent_id IS NULL
  UNION
  SELECT my.categories.id, my.categories.parent_id || t.parentlist
    FROM my.categories 
    JOIN t ON categories.parent_id = t.id
) SELECT * FROM t
-- outputs:
-- id  | parentlist
-- ----+------------
-- 1   | {}
-- 2   | {1}
-- 3   | {1}
-- 4   | {2,1}
-- 5   | {2,1}
-- 6   | {3,1}

If you want to get a record of the parents of one id you just need to change the query like:

WITH RECURSIVE t(id, parentlist) AS (
  SELECT id, ARRAY[]::bigint[] FROM my.categories WHERE parent_id IS NULL
  UNION
  SELECT my.categories.id, my.categories.parent_id || t.parentlist
    FROM my.categories 
    JOIN t ON categories.parent_id = t.id
) SELECT unnest(parentlist) as parents_ids FROM t WHERE id=6;
-- outputs:
-- parents_ids
-- -----------
-- 3
-- 1

Note that the last query does not output the "current" id (6).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...