I have three tables. Think of them as the following:
Recipes
id | name
1 | Cookies
2 | Soup
...
Ingredients
id | name
1 | flour
2 | butter
3 | chicken
...
Recipe_Ingredient
recipe_id | ingredient_id
1 | 1
1 | 2
2 | 3
Hopefully you get the idea. What I'd like is a query where I can find all recipes that have ingredients that are a subset of a given set of ingredients.
The idea being that I'd like to have a list of all the things I can make with what I have on hand (but of course not EVERYTHING I have on hand.)
I tried implementing this with various levels of subqueries and correlated subqueries with EXISTS but had no luck. I also tried to use HAVING and COUNT, but that only seems to work for me if I want something that uses ALL the ingredients I have on hand.
Test this if it works. Assuming that you have your (unique) ingredient ids available in a table called ingredients_avail, this query should show the recipe id that has complete ingredients:
Additionally, here it is in a more typical syntax: