Skip to content
Advertisement

Mysql many to many relations. Filter exact match

My schema

Hello. I want to select dishes which has the only the exactly selected ingredient alias? For example: Ingredient alias table:

Ingredient table:

Table Meal:

Table meal_ingredient:

How I can get meals only which has ingredients only with ingredient aliases Potato and chicken? In my example, the result must be a meal with id 3 Meal with Chicken 2 Potato 1 ?

Advertisement

Answer

You need to join all 4 tables, group by meal and put the condition in the HAVING clause:

See the demo.
Results:

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