有如下表:
产品表:product(product_id,product_name);
分类表:categories(categories_id,parent_id,categories_name);
关系表:product_categories(id,product_id,categories_id); 其中categories的parent_id外键为categories的categories_id,例如:A(1,0,电影) A1(2,1,动作片) A2(3,1,科幻片)A11(4,2,国外动作片),则A1,A2是A的子categories,而A11是A1的子categories。
问题:若条件知道categories_id值,求该categories_id的categories下的全部产品信息,求该SQL(能用一条SQL实现吗?)。
注意:只有categories再没有子categories的情况下,该categories才能有产品,否则该categories下只能是子categories。(可以看作树形结构,只有没有子节点的节点才能有产品信息)
产品表:product(product_id,product_name);
分类表:categories(categories_id,parent_id,categories_name);
关系表:product_categories(id,product_id,categories_id); 其中categories的parent_id外键为categories的categories_id,例如:A(1,0,电影) A1(2,1,动作片) A2(3,1,科幻片)A11(4,2,国外动作片),则A1,A2是A的子categories,而A11是A1的子categories。
问题:若条件知道categories_id值,求该categories_id的categories下的全部产品信息,求该SQL(能用一条SQL实现吗?)。
注意:只有categories再没有子categories的情况下,该categories才能有产品,否则该categories下只能是子categories。(可以看作树形结构,只有没有子节点的节点才能有产品信息)
你是不是要查产品信息及它的完整分类路径?
where b.categories_id in
(select categories_id from categories connect by categories_id = parent_id
start with categories_id = 'id值')
product_name
FROM product pro,
product_categories pc
WHERE pro.product_id = pc.product_id
AND pc.categories_id IN (
SELECT cat.categories_id
FROM categories cat
START WITH cat.categories_id = 'id'
CONNECT BY PRIOR cat.categories_id = cat.parent_id
)