合并自引用连接的 Mysql 查询

发布于 2024-09-14 11:50:19 字数 700 浏览 13 评论 0原文

假设我有四个表:

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

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

Say I have four tables:

------------- 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    
-------------------------------------

category has many (sub)categories, through self referential join subcategories

A feature has many categories, of those some will be subcategories, through join feature_categories

What I need is to send an array of feature ids and a (main) category id and have returned all subcategories. This is proving more difficult than I would have hoped so I'd appreciate greatly any help. Let me know if this question isn't clear.

EDIT
I don't need the feature table to be included in any query. In facet, I only require the subcategory (category) name field to be returned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

留蓝 2024-09-21 11:50:19

必须解决这个问题,但下面的任何一个都应该可行。第二个可能更有效:

 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,...)

Had to work through this a bit, but either below should work. 2nd one is probably more efficient:

 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

or:

    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,...)
喜爱皱眉﹌ 2024-09-21 11:50:19

以下内容可以完成这项工作吗?

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;

does the following do the job?

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;
仲春光 2024-09-21 11:50:19
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,...)

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

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,...)

If I understand correctly what you are asking...

枉心 2024-09-21 11:50:19
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;
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;
冷了相思 2024-09-21 11:50:19

这可能会给出预期的结果

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
             )

This may give expected result

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
             )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文