包含自引用连接的Mysql查询

时间:2010-08-16 11:27:51

标签: mysql join self-reference

说我有四张桌子:

------------- 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并返回所有子类别。事实证明这比我希望的更难,所以我非常感谢任何帮助。如果这个问题不明确,请告诉我。

EDIT 我不需要将功能表包含在任何查询中。在facet中,我只需要返回子类别(类别)名称字段。

5 个答案:

答案 0 :(得分: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,...)

答案 1 :(得分: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;

答案 2 :(得分: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,...)

如果我理解你的要求......

答案 3 :(得分: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;

答案 4 :(得分: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
             )