如何获取父子结构的根ID

时间:2011-07-07 14:12:22

标签: sql ms-access

鉴于产品ID,我需要找到该产品所在的最顶级类别。

有3个顶级类别,我们称之为 A B C

我有一个包含所有类别的Categories表。这包括类别 A B C 以及所有子类别,例如 foo bar < / strong>等...

我还有一个名为ProductCategories的表。此表包含每种产品的参考以及该产品所属的类别。如果产品在下分类, Foo 的子类别是 B 的子类别,则结构为B -> Foo -> Bar产品有问题的ProductCategories表中有3个条目。

这可能更好地解释了它:

Categories
+--------------------------------+
| ID | Name           | ParentID |
+----+----------------+----------+
| 1  | B              | Null     |
+----+----------------+----------+
| 2  | Foo            | 1        |
+----+----------------+----------+
| 3  | Bar            | 2        |
+----+----------------+----------+
| 4  | A              | Null     |
+----+----------------+----------+
| 5  | Subcategory    | 4        |
+----+----------------+----------+
| 6  | AnotherSubCat  | 5        |
+----+----------------+----------+
| 7  | SoManySubCats  | 6        |
+----+----------------+----------+

ProductCategories
+-----------+----------------+
| ProductID | ParentCategory |
+-----------+----------------+
| 50        | 2              |    // Product 50 would be:
+-----------+----------------+    // B -> Foo -> Bar
| 50        | 1              |
+-----------+----------------+
| 50        | 3              |
+-----------+----------------+ 
| 89        | 5              |    // Product 89 would be:
+-----------+----------------+    // A -> Subcategory -> AnotherSubCat -> SoManySubCats
| 89        | 4              |
+-----------+----------------+
| 89        | 7              |
+-----------+----------------+
| 89        | 6              |
+-----------+----------------+

关于这个数据库结构,我无能为力。

我无法弄清楚如何编写查询,我可以提供产品ID,它会告诉我最重要的类别, A B C

希望有人可以对此有所了解。

哦,是的,我正在使用MS Access 2003。

2 个答案:

答案 0 :(得分:2)

我在散文中描述的查询:将产品的类别与类别表一起加入,并选择具有NULL的类别,因为它是ParentID。

SELECT Categories.Name 
FROM Categories, ProductCategories 
WHERE ProductCategories.ParentCategory = Categories.ID 
  AND ProductCategories.ProductID = 50
  AND Categories.ParentID IS NULL

答案 1 :(得分:0)

<pre>
Declare @Categories Table 
(
     ID int,[Name] varchar(100),ParentID int
)
insert into @Categories
Select 1,'B',Null UNION ALL
Select 2,'Foo',1  UNION ALL
Select 3,'Bar',2  UNION ALL
Select 4,'A',Null  UNION ALL
Select 5,'Subcategory',4  UNION ALL
Select 6,'AnotherSubCat',5  UNION ALL
Select 7,'SoManySubCats',6 

Declare @ProductCategories Table
(
    ProductID int,ParentCategory int
)
insert into @ProductCategories
Select  50,2  UNION ALL
Select  50,1  UNION ALL
Select  50,3  UNION ALL
Select  89,5  UNION ALL
Select  89,4 

;with CTE AS
(
    SELECT ID,[Name],ParentId,P.ProductID,
    Cast(ID As Varchar(Max)) As ReportingID,
    Cast([Name] As Varchar(Max)) As ReportingName
    From @Categories As C
    INNER JOIN @ProductCategories As P On C.ID=P.ParentCategory
    Where ParentId is null

    UNION ALL

    SELECT C.ID,C.[Name],C.ParentId,P.ProductID,
    Cast(CTE.ReportingID+'->'+Cast(C.ID As Varchar(10)) As Varchar(Max)) As ReportingID,
    Cast(CTE.ReportingName+'->'+C.[Name] As Varchar(Max)) As ReportingName
    From @Categories As C
    INNER JOIN @ProductCategories As P On C.ID=P.ParentCategory
    INNER JOIN CTE ON C.ParentId=CTE.ID 
)
Select *
From CTE

--now add where cluase to get the reportingname


</pre>