Jump to content

Recommended Posts

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.

 

The closest I have thus far is:

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 (1,2,3,4)

However this doesn't filter the subcategories by their having a main category.

 

Help would be very much appreciated as I really want to get this signed off and it is completely alluding me.

I will suggest to implement your categories - subcategories in a different and easy way

 

Categories

 

cat_id  INT,

cat_name  VARCHAR,

cat_parent INT

 

for Master Categories the field cat_parent should be 0 or NULL  for Subcategories the cat_parent field will point to the Master category (cat_id)... this should simplify your query.

 

 

Hi Mikosiko and thanks for your reply. The only problem with that is a subcategory cannot belong to two categories, as is required.

 

The solution I currently have is:

    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 (@feature_ids)
        AND c.id = @main_category_id
        and fc2.feature_id in (@feature_ids)

If anyone has a better solution I'd be glad to hear it.

 

:)

Wrong solution posted:

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.