I have two tables, Categories and RecipeCategories
**Categories**
CategoryID varchar (Primary Key)
Name varchar
**RecipeCategories**
RecipeID varchar
CategoryID varchar
Composite primary key
I’m searching for a way to return CategoryID and Name for all categories, plus whether or not the recipe is actually in that category. I could use something like
SELECT c.CategoryID, c.Name,
(SELECT COUNT(*)
FROM RecipeCategories
WHERE RecipeID = @recipeId AND CategoryID = c.CategoryID))
FROM Categories c
But I don’t think that would scale if the table gets too big.
1 Answer