Skip to content
Advertisement

MYSQL – Fetching entities whose many-to-many relationship contains all terms collectively

I am using the following DB Schema

and using something like this to add data/query it

which returns

How can I return recipes who’s ingredients are a subset of a list of ingredients provided?

e.g I should not return Chocolate cake if list (egg, salt, sugar) is provided. However I should return Chocolate cake if (egg, salt, ham, cheese, sugar, sausage, chocolate) is provided.

Advertisement

Answer

You can use aggregation and a having clause:

You control the elements the the ingredients should be a subset of with the list of values in the right operand to the IN conditions.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement