I have the following 3 tables in my database, and I have some problems querying them for the results I want. I am trying to find recipes for ingredients.
SQL script script below: fiddle
Here are my tables: Ingredients
+---------------+---------+ | ingredient_id | name | +---------------+---------+ | 1 | tomato | | 2 | onion | | 3 | rice | | 4 | chicken | | 5 | beef | | 6 | noodles | | 7 | salt | +---------------+---------+
Recipes
+-----------+------------------+ | recipe_id | name | +-----------+------------------+ | 1 | tomato goodness | | 2 | meat deluxe | | 3 | chicken surprise | +-----------+------------------+
Ingredient_Index
+-----------+---------------+ | recipe_id | ingredient_id | +-----------+---------------+ | 1 | 1 | | 1 | 5 | | 1 | 7 | | 2 | 5 | | 2 | 6 | | 2 | 7 | | 3 | 4 | | 3 | 3 | | 3 | 7 | +-----------+---------------+
a search query for only one ingredient works fine and outputs this:
mysql> select r.recipe_id, r.name -> from recipes r -> inner join ingredient_index -> on i.recipe_id = r.recipe_id -> where -> i.ingredient_id = 7; +-----------+------------------+ | recipe_id | name | +-----------+------------------+ | 1 | tomato goodness | | 2 | meat deluxe | | 3 | chicken surprise | +-----------+------------------+
But when using or for several ingredients we get this
mysql> select r.name -> from recipes r -> inner join ingredient_index i -> on i.recipe_id = r.recipe_id -> where i.ingredient_id = 7 or i.ingredient_id = 5; +------------------+ | name | +------------------+ | tomato goodness | | tomato goodness | | meat deluxe | | meat deluxe | | chicken surprise | +------------------+
5 lines per set (0.00 sec)
and using "and" results without anything
mysql> select r.name -> from recipes r -> inner join ingredient_index i -> on i.recipe_id = r.recipe_id -> where i.ingredient_id = 7 and i.ingredient_id = 5; Empty set (0.00 sec)
Any help would be greatly appreciated!