1

I have the below query which works as I need it, but I also want to add the name of the ParentCategoryID in another column which would need to custom as there is no column for ParentCategory name. The only way I can get that is by referencing the CategoryID that I retrieved from t.ParenCategoryID. CategoryID and ParentCategoryID are in the same table. Can this be accomplished a sub query?

SELECT c.ContentID
    ,c.Title
    ,t.CategoryID
    ,t.Name
   ,t.ParentCategoryID
FROM Content c
INNER JOIN ContentCategory g ON c.ContentID = g.ContentID
RIGHT JOIN Category t ON g.CategoryID = t.CategoryID
ORDER BY g.CategoryID

Thanks in advance.

1 Answer 1

3
SELECT c.ContentID
       ,c.Title
       ,t.CategoryID
       ,t.Name
       ,t.ParentCategoryID
       ,tc.Name AS ParentCategoryName
FROM   Content c
       INNER JOIN ContentCategory g 
           ON c.ContentID = g.ContentID
       RIGHT JOIN Category t 
           ON g.CategoryID = t.CategoryID
       LEFT JOIN Category tc 
           ON t.CategoryID = tc.ParentCategoryID
ORDER BY g.CategoryID
Sign up to request clarification or add additional context in comments.

2 Comments

It's very confusing to see INNER...RIGHT...LEFT.
Thanks. The name results for the parent category name are not matching, but I think that's a byproduct of my initial query. I did a similar query using different tables and it worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.