查询以选择活动目的地类别的子类别?

时间:2010-08-17 18:27:18

标签: sql mysql join

我有一个目的地(州)表,其中包含活动的主要级别类别和子类别。我的最终目标是输出xml导航链接,以便链接到:

  1. 每个目标固定链接(按目标slug名称按字母顺序排序)
  2. 下的每个类别
  3. 类别
  4. 下的每个子类别

    下面是我将使用sql查询生成的内容,因为它对查询有任何影响,但我不需要xml帮助来解决这个问题

    <item href="/destinations/alabama/">
        <list>
            <item href="/destinations/alabama/category/">
                <list>
                   <item href="/destinations/alabama/category/sub-category/"></item>
                </list>
            </item>
        </list>
    </item>
    <item href="/destinations/maryland/">
        <list>
            <item href="/destinations/maryland/category/">
                <list>
                   <item href="/destinations/maryland/category/sub-category/"></item>
                </list>
            </item>
        </list>
    </item>
    

    目前我有一个查询来获取子类别,但它没有考虑活动目的地。

    SELECT
    
    subcategories.name AS subcategory_name,
    subcategories.slug AS subcategory_slug,
    categories.name AS category_name,
    categories.slug AS category_slug
    
    FROM
    
    subcategories
    
    LEFT JOIN destinations_subcategories ON
    destinations_subcategories.subcategory_id = subcategories.id
    
    LEFT JOIN categories ON
    destinations_subcategories.category_id = categories.id
    
    WHERE
    1=1 AND
    subcategories.is_active = 1 AND
    categories.is_active = 1
    

    返回:

    subcategory_name    subcategory_slug    category_name    category_slug
    Fly Fishing         fly-fishing         Fishing          fishing
    

    但是此查询不考虑活动的目标,因为可以映射非活动目标和活动类别之间的关系。我想我可能需要一个子查询抓住所有活动目的地,然后加入?





    SCHEMA /行样本:

    id    name       slug       active
    7     Maryland   maryland   1
    

    我的类别表,示例行:

    id    name       slug       active
    1     Fishing    fishing    1
    

    子类别表格样本行:

    id    name           slug           active
    3     Fly-Fishing    fly-fishing    1
    

    和2个表来存储关系,第一个将子类别映射到类别:

    id    category_id    subcategory_id
    5     1              3
    

    第二个destinations_subcategories将目的地映射到子类别:

    id    destination_id   category_id    subcategory_id
    5     7                1              3
    

2 个答案:

答案 0 :(得分:1)

这个怎么样

SELECT

subcategories.name AS subcategory_name,
subcategories.slug AS subcategory_slug,
categories.name AS category_name,
categories.slug AS category_slug

FROM

destination 

INNER JOIN destinations_subcategories on destination.id = destinations_subcategories.destination_id and destination.active = 1

LEFT JOIN subcategories ON destinations_subcategories.subcategory_id = subcategories.id

LEFT JOIN categories ON destinations_subcategories.category_id = categories.id

WHERE 1=1 
AND subcategories.is_active = 1 
AND categories.is_active = 1

这将确保只显示活动目的地

答案 1 :(得分:1)

SELECT destination.name , categories.name, sub-category.name 
FROM
destination_subcategories as ds, destinations, categories,  subcategories 
WHERE
ds.destination_id = destination.id and ds.catrgoy_id = category.id and ds.sub_category_id = sub_category.id and destination.is_active = 1 and categories.is_active = 1 and subcategory.is_active = 1;

这似乎应该有效,除非我误解了架构。