Right now I am using sqlite within a ios application, and I want to be able to search for recipes that can be made from a list of ingredients (ie recipes such that are a subset of the provided ingredients)
For example:
Recipe 1: A B C
Recipe 2: A B
Recipe 3: C D
Recipe 4: A
Recipe 5: E
Query for ingredients A B C returns recipes {1, 2, 4}
Query for ingredients A B returns recipes {2, 4}
Query for ingredients D returns {}
Currently what I have set up is
Table Items
name text primary key
Table Recipes
ID required_item integer, recipe_name text
I can easily do queries for recipes containing any of the three ingredients and queries for recipes containing all of the three ingredients, but I haven’t been able to figure out how to do queries for only
Recipes ∈ P(Ingredients)
Sorry, I’m new to database programming, and any help would be greatly appreciated
You could use a
left jointo a search table. Thengroup byrecipe. Use ahavingclause to demand that for each recipe, there are no ingredients that are not in the search list. For example:Live example at SQL Fiddle.