-1

我想获取 CategoryID(1) ;如果其子类别(2)在内容表中。

我的意思是:从 contenttable 中获取 categoryid (2);在 categorytable 中找到它的 parentid(1);并选择顶级类别行(categoryid:1) - 所以我无法选择内容的顶级类别行,我该怎么做?

分类表:

CategoryID , ParentID
    1            0
    2            1

内容表:

  _id, CategoryID
  ...       2 

代码:

   SELECT DISTINCT contenttable.CategoryID, categorytable.CategoryID FROM categorytable 
                    INNER JOIN contenttable 
                    ON categorytable.CategoryID = contenttable.CategoryID
4

2 回答 2

1
SELECT parent.CategoryID
FROM categorytable AS parent
JOIN categorytable AS child ON child.ParentID = parent.CategoryID
JOIN contenttable           ON child.CategoryID = contenttable.CategoryID
于 2013-04-26T07:30:02.930 回答
-1

你的问题还不清楚。您正在寻找此查询吗?

select CategoryID.categorytable from categorytable where CategoryID.categorytable=(select ParentID.categorytable from categorytable where CategoryID.categorytable=(select CategoryID.contenttable from contenttable));
于 2013-04-26T07:21:42.297 回答