PostgreSQL 从 table 获取父类别
PostgreSQL get parent categories from table
我有 table 如下所示。
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' |
+----+-----------+---------------+
我需要获取父类别的所有 ID。
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;
带有数据的示例结果,但实际上我只需要 id。
+----+-----------+---------------+
| id | parent_id | name |
+----+-----------+---------------+
| 1 | null | 'LEVEL 1' |
| 3 | 1 | 'LEVEL 2.2' |
| 6 | 3 | 'LEVEL 3.2.1' |
+----+-----------+---------------+
或者像这样:
+----+-----------+---------------+
| id | parent_id | name |
+----+-----------+---------------+
| 3 | 1 | 'LEVEL 2.2' |
| 6 | 3 | 'LEVEL 3.2.1' |
+----+-----------+---------------+
问题是我不允许使用程序。此查询应用作许多其他查询的子查询。并且请不要看 name
无关紧要的专栏。
如果我找到你,这就是你需要的。
首先,通过以下查询,您可以获得所有 parent 个 ID:
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}
如果你想获得一个 id 的 parents 的记录,你只需要像这样更改查询:
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
请注意,最后一个查询不会输出 "current" id (6)。
我有 table 如下所示。
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' |
+----+-----------+---------------+
我需要获取父类别的所有 ID。
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;
带有数据的示例结果,但实际上我只需要 id。
+----+-----------+---------------+
| id | parent_id | name |
+----+-----------+---------------+
| 1 | null | 'LEVEL 1' |
| 3 | 1 | 'LEVEL 2.2' |
| 6 | 3 | 'LEVEL 3.2.1' |
+----+-----------+---------------+
或者像这样:
+----+-----------+---------------+
| id | parent_id | name |
+----+-----------+---------------+
| 3 | 1 | 'LEVEL 2.2' |
| 6 | 3 | 'LEVEL 3.2.1' |
+----+-----------+---------------+
问题是我不允许使用程序。此查询应用作许多其他查询的子查询。并且请不要看 name
无关紧要的专栏。
如果我找到你,这就是你需要的。
首先,通过以下查询,您可以获得所有 parent 个 ID:
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}
如果你想获得一个 id 的 parents 的记录,你只需要像这样更改查询:
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
请注意,最后一个查询不会输出 "current" id (6)。