select 所有产品并通过子类别加入主类别(级别未知)
select all products and join main category through sub-categories (unknown level)
我有 2 table
类别
id - name - parent
1 - Category A - 0
2 - Category B - 0
3 - Category C - 0
4 - Category D - 0
5 - Subcategory Of 1 - 1
6 - Subcategory Of 5 - 5
7 - Subcategory Of 5 - 5
产品
id - name - category - description
1 - Name - 5 - Description
如何select所有商品通过子类目加入主类目?产品类别只能有 1 或 2 或 3 或 4 级别(未知级别)。
我在类别 table 中使用 "WITH RECURSIVE",但找不到将产品 table 与 1 次查询组合的方法
WITH RECURSIVE category_child AS
(
SELECT * FROM categories as c WHERE c.id = 5
UNION ALL
SELECT c2.* FROM categories AS c2
JOIN category_child as c3 ON c3.parent_id = c2.id
)
最好的方法是什么?
预期结果
id - name - category - description - root - sub category id 1 - sub category id 2 - sub category id 3
或
id - name - category - description - root
id - name - category - description - sub category id 1
id - name - category - description - sub category id 2
id - name - category - description - sub category id 3
因为你想要一个类别的完整路径,你不能用 c.id = 5
开始你的非递归部分你必须使用 where parent_id is null
从根开始(你应该 not 使用不存在的类别 ID 识别根节点,这会阻止为 parent_id 列创建正确的外键)。
在递归部分,您可以将完整路径聚合到根类别:
with recursive tree as
(
select *, id as root_category, concat('/', name) as category_path
from category
where parent_id is null
union all
select c.*, p.root_category, concat(p.category_path, '/', c.name)
from category c
join tree p on c.parent_id = p.id
)
select p.id as product_id,
p.name as product_name,
t.root_category,
t.category_path
from tree t
join product p on p.category = t.id
使用以下示例数据:
create table category (id integer, name text, parent_id integer);
create table product (id integer, name text, category integer, description text);
insert into category
values
(1, 'Category A', null),
(2, 'Category B', null),
(3, 'Category C', null),
(4, 'Category D', null),
(5, 'Subcategory Of 1', 1),
(6, 'Subcategory Of 5', 5),
(7, 'Subcategory Of 5', 5),
(8, 'Subcategory of D', 4)
;
insert into product
values
(1, 'Product One', 5, 'Our first product'),
(2, 'Product Two', 8, 'The even better one');
这个returns:
product_id | product_name | root_category | category_path
-----------+--------------+---------------+-----------------------------
1 | Product One | 1 | /Category A/Subcategory Of 1
2 | Product Two | 4 | /Category D/Subcategory of D
我有 2 table
类别
id - name - parent 1 - Category A - 0 2 - Category B - 0 3 - Category C - 0 4 - Category D - 0 5 - Subcategory Of 1 - 1 6 - Subcategory Of 5 - 5 7 - Subcategory Of 5 - 5
产品
id - name - category - description 1 - Name - 5 - Description
如何select所有商品通过子类目加入主类目?产品类别只能有 1 或 2 或 3 或 4 级别(未知级别)。
我在类别 table 中使用 "WITH RECURSIVE",但找不到将产品 table 与 1 次查询组合的方法
WITH RECURSIVE category_child AS ( SELECT * FROM categories as c WHERE c.id = 5 UNION ALL SELECT c2.* FROM categories AS c2 JOIN category_child as c3 ON c3.parent_id = c2.id )
最好的方法是什么?
预期结果
id - name - category - description - root - sub category id 1 - sub category id 2 - sub category id 3
或
id - name - category - description - root id - name - category - description - sub category id 1 id - name - category - description - sub category id 2 id - name - category - description - sub category id 3
因为你想要一个类别的完整路径,你不能用 c.id = 5
开始你的非递归部分你必须使用 where parent_id is null
从根开始(你应该 not 使用不存在的类别 ID 识别根节点,这会阻止为 parent_id 列创建正确的外键)。
在递归部分,您可以将完整路径聚合到根类别:
with recursive tree as
(
select *, id as root_category, concat('/', name) as category_path
from category
where parent_id is null
union all
select c.*, p.root_category, concat(p.category_path, '/', c.name)
from category c
join tree p on c.parent_id = p.id
)
select p.id as product_id,
p.name as product_name,
t.root_category,
t.category_path
from tree t
join product p on p.category = t.id
使用以下示例数据:
create table category (id integer, name text, parent_id integer);
create table product (id integer, name text, category integer, description text);
insert into category
values
(1, 'Category A', null),
(2, 'Category B', null),
(3, 'Category C', null),
(4, 'Category D', null),
(5, 'Subcategory Of 1', 1),
(6, 'Subcategory Of 5', 5),
(7, 'Subcategory Of 5', 5),
(8, 'Subcategory of D', 4)
;
insert into product
values
(1, 'Product One', 5, 'Our first product'),
(2, 'Product Two', 8, 'The even better one');
这个returns:
product_id | product_name | root_category | category_path
-----------+--------------+---------------+-----------------------------
1 | Product One | 1 | /Category A/Subcategory Of 1
2 | Product Two | 4 | /Category D/Subcategory of D