2

假设我有四张桌子:

------------- features --------------
id: int
name: varchar
-------------------------------------

-------- feature_categories ---------
feature_id: int
category_id: int    
-------------------------------------

----------- categories --------------
id: int
name: varchar    
-------------------------------------

------ category_subcategories -------
category_id: int
sub_category_id: int    
-------------------------------------

类别有很多(子)类别,通过自引用连接子类别

一个特征有很多类别,其中一些是子类别,通过加入 feature_categories

我需要的是发送一组特征 ID 和一个(主)类别 ID,并返回所有子类别。事实证明,这比我希望的要困难得多,所以我非常感谢任何帮助。如果这个问题不清楚,请告诉我。

编辑我不需要将特征表包含在任何查询中。在方面,我只需要返回子类别(类别)名称字段。

4

5 回答 5

1

不得不解决这个问题,但下面的任何一个都应该工作。第二个可能更有效:

 select f.name featureName, 
  c.name CategoryName, 
  c2.name SubCategoryName  
    FROM features f, feature_categories fc, categories c, category_subcategories sc, categories c2, feature_categories fc2
    WHERE f.id = fc.feature_id
    AND c.id = fc.category_id
    AND sc.category_id = c.id
    and c2.id = sc.sub_category_id
    and fc2.category_id = c2.id
    AND f.id in (0,1,2,...)
    and fc2.feature_id in (0,1,2,...)
    AND c.id = @main_category_id

或者:

    select f.name featureName, 
  c.name CategoryName, 
  c2.name SubCategoryName  
        FROM features f 
        inner join feature_categories fc on f.id = fc.feature_id
        inner join categories c on c.id = fc.category_id
        inner join category_subcategories sc on sc.category_id = c.id
        inner join categories c2 on c2.id = sc.sub_category_id
        inner join feature_categories fc2 on fc.category_id = c2.id

        WHERE f.id in (0,1,2,...)
        AND c.id = @main_category_id

        and fc2.feature_id in (0,1,2,...)
于 2010-08-18T20:50:42.353 回答
0

这可能会产生预期的结果

select cat.name
  from categories cat,
       feature_categories feacat,
       category_subcategories cat_subcat
 where feacat.feature_id in (1,2,3)
   and feacat.category_id = cat.id
   and exists(select 1 
                from cat_subcat 
               where category_id = @catid 
                 and sub_category_id=cat.id
             )
于 2010-08-24T09:50:34.610 回答
0
select f.name featureName,
  c.name CategoryName,
  sc.name SubCategoryName 
from features f
join features_categories fc on f.id=fc.feature_id
join categories c on fc.category_id=c.id
join category_subcategories cs on c.id=cs.category_id
join categories sc on cs.sub_categories_id=sc.id
WHERE f.id IN (0,1,2,...)

如果我理解正确你在问什么...

于 2010-08-18T13:01:21.370 回答
0

以下做这项工作吗?

SELECT *
FROM `category_subcategories` sc
  JOIN `categories` c ON sc.category_id = c.id
  JOIN `feature_categories` fc ON fc.category_id = c.id
WHERE fc.feature_id IN (0,1,2,...)
  AND c.id = main_category_id;
于 2010-08-16T11:36:00.013 回答
0
select f.name featureName, 
c.name CategoryName, 
c2.name SubCategoryName  
FROM features f 
inner join feature_categories fc on f.id = fc.feature_id
inner join categories c on c.id = fc.category_id
inner join category_subcategories sc on sc.category_id = c.id
inner join categories c2 on c2.id = sc.sub_category_id
inner join feature_categories fc2 on fc.category_id = c2.id and fc2.feature_id = fc.feature_id
WHERE f.id in (0,1,2,...)
AND c.id = @main_category_id;
于 2010-08-19T12:36:16.340 回答