开发者

Query to select subcategories of categories of destinations which are active?

开发者 https://www.devze.com 2023-01-12 12:29 出处:网络
I have a table of destinations ( states ) which contain primary level categories and subcategories for activities. My ultimate goal is to output xml navigation links so I can link to:

I have a table of destinations ( states ) which contain primary level categories and subcategories for activities. My ultimate goal is to output xml navigation links so I can link to:

  1. Each destination permalink ( ordered alphabetically by destination slug name )
  2. Each category underneath
  3. Each subcategories underneath the category

The below is what I'll generate with the sql query, incase it has any bearing on the query but I don't need xml help for this question:

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

Currently I have a query that grabs subcategories but it doesn't account for active destinations.

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

Which returns:

subcatego开发者_如何学JAVAry_name    subcategory_slug    category_name    category_slug
Fly Fishing         fly-fishing         Fishing          fishing

But this query doesn't account for destinations which are active, since it's possible to map relationships between inactive destinations and active categories. I'm thinking I need to maybe have a subquery grabbing all active destinations, then do the joining?

SCHEMA/row samples:

id    name       slug       active
7     Maryland   maryland   1

My categories table, sample row:

id    name       slug       active
1     Fishing    fishing    1

Subcategories table sample row:

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

And 2 tables to store relationships, the first maps subcategories to categories:

id    category_id    subcategory_id
5     1              3

The second, destinations_subcategories maps destinations to subcategories:

id    destination_id   category_id    subcategory_id
5     7                1              3


how about this

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

This would make sure that only active destinations are shown


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;

This looks like should work unless, i've misunderstood the schema.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号