Hello,
I have a SQL question. I'm working with the following set of tables:
The Recipe table contains 5 recipes, Ingredient contains many ingredients (52), and IngredientList is an associative entity, an implementation of the many-to-many relationship between Recipe and Ingredient. RecipeID and IngredientID are essentially auto-increment ID numbers.
I'm trying to write a query that displays all vegetarian recipes (that is, recipes without
'%beef%',
'%pork%',
'%chicken%',
'%lamb%
in any of their associated Ingredient.Name). The issue I'm running into is that the result set contains one row per ingredient, rather than one row per recipe.
The query that generates this result set is (note that the Ingredient table is named ingredients here):
SELECT recipe.name AS Dish, ingredients.name
FROM (recipe JOIN ingredientlist ON recipe.recipeid=ingredientlist.recipeid
JOIN ingredients ON ingredientlist.ingredientid=ingredients.ingredientid)
WHERE ingredients.name NOT LIKE '%beef%'
AND ingredients.name NOT LIKE '%chicken%'
AND ingredients.name NOT LIKE '%pork%'
AND ingredients.name NOT LIKE '%lamb%';
That query generates 50 results (not 52, because two ingredients are excluded based on their ingredient.name containing the a substring excluded by my WHERE clause).
My goal is to return 3 of the 5 recipe names in the recipe table (subtracting those containing meat in the associated ingredients.name).