I am trying to implement a webservice that will take as input some ingredients, it will look up some recipies in a database and will return those ones that require all or some of the ingredients provided as input.
I can easily figure out the many to many relationship between the recipies and ingrdients so I am planning to have a table for recipies, one for ingredients and a link table in the middle. However, if I do this I am not sure how I can query the tables in an efficient way to get the information required.
To give an explicit example consider the following:
Input: Pasta, Chicken, Pesto, Tomatoes
Output should be:
- Pasta with Chicken in Tomato sauce (requiring Pasta, Chicken and tomatoes)
- Pasta with Pesto in the oven (requiring Pasta and Pesto)
- etc.
But output should not include Chicken with potatoes in tomato sause (since we do not have potatoes in the input ingredients).
I will be implementing this in Java and MySQL (although the Java part is probably not important for this question).
Can somebidy help me in approaching this. I am really after how to implement the Query but if you can think of other ways to approach it then these are welcome too.
Thanks in advance
First, put your list of ingredients that are “desired” into a temporary table. Let’s call this “desired”.
The following query will find recipes that have only ingredients in the desired table: