How can I find all the rows in a table that exclusively have related rows in a given list?

StackOverflow https://stackoverflow.com/questions/9953855

  •  28-05-2021
  •  | 
  •  

Domanda

I have three tables. Think of them as the following:

  Recipes
  id | name
  1  | Cookies
  2  | Soup
  ...


  Ingredients
  id | name
  1  | flour
  2  | butter
  3  | chicken
  ...

  Recipe_Ingredient
  recipe_id | ingredient_id
  1         | 1
  1         | 2
  2         | 3

Hopefully you get the idea. What I'd like is a query where I can find all recipes that have ingredients that are a subset of a given set of ingredients.

The idea being that I'd like to have a list of all the things I can make with what I have on hand (but of course not EVERYTHING I have on hand.)

I tried implementing this with various levels of subqueries and correlated subqueries with EXISTS but had no luck. I also tried to use HAVING and COUNT, but that only seems to work for me if I want something that uses ALL the ingredients I have on hand.

È stato utile?

Soluzione

Test this if it works. Assuming that you have your (unique) ingredient ids available in a table called ingredients_avail, this query should show the recipe id that has complete ingredients:

    SELECT recipe_id
    FROM [select recipe_id, count(*) as num_of_ingredients from recipe_ingredient group by recipe_id]. AS x, [select recipe_ingredient.recipe_id as recipe_id, count(*) as num_of_ingredients 
    from recipe_ingredient, ingredients_avail               
    where               
     recipe_ingredient.ingredient_id = ingredients_avail.ingredient_id
    group by recipe_ingredient.recipe_id]. AS y               
    WHERE x.recipe_id = y.recipe_id and               
    x.num_of_ingredients = y.num_of_ingredients;               

Additionally, here it is in a more typical syntax:

  SELECT x.recipe_id 
    FROM ( 
      SELECT recipe_id, count(*) as num_of_ingredients 
        FROM recipe_ingredients
      GROUP BY recipe_id
     ) x, ( 
      SELECT recipe_id, count(*) as num_of_ingredients 
        FROM recipe_ingredients
        JOIN ingredients_avail 
          ON recipe_ingredients.ingredient_id = ingredients_avail.ingredient_id 
      GROUP BY recipe_id
     ) y 
  WHERE x.recipe_id = y.recipe_id AND x.num_of_ingredients = y.num_of_ingredients;

Altri suggerimenti

    mysql>
    mysql> select * from ingredients;
    +------+---------------+-----------+
    | id   | name          | available |
    +------+---------------+-----------+
    |    1 | salt          | n         |
    |    2 | sugar         | n         |
    |    3 | flour         | n         |
    |    4 | butter        | n         |
    |    5 | vanilla       | n         |
    |    6 | baking powder | n         |
    |    7 | egg           | n         |
    +------+---------------+-----------+
    7 rows in set (0.00 sec)

    mysql> select * from recipes;
    +------+---------------+
    | id   | name          |
    +------+---------------+
    |    1 | cookie        |
    |    2 | soup          |
    |    3 | xtreme flavor |
    +------+---------------+
    3 rows in set (0.00 sec)

    mysql> select * from recipe_ingredient;
    +-----------+---------------+
    | recipe_id | ingredient_id |
    +-----------+---------------+
    |         1 |             1 |
    |         1 |             2 |
    |         1 |             3 |
    |         1 |             4 |
    |         1 |             5 |
    |         1 |             6 |
    |         1 |             7 |
    |         2 |             1 |
    |         2 |             7 |
    |         3 |             4 |
    |         3 |             3 |
    +-----------+---------------+
    11 rows in set (0.00 sec)

    mysql>
    mysql> update ingredients set available = 'n';
    Query OK, 0 rows affected (0.00 sec)
    Rows matched: 7  Changed: 0  Warnings: 0

    mysql>
    mysql> update ingredients set available = 'y'
        -> where id in  (1,2,3,4,5,6,7);
    Query OK, 7 rows affected (0.00 sec)
    Rows matched: 7  Changed: 7  Warnings: 0

    mysql>
    mysql> select recipes.name from
        -> (select recipe_id, available from
        -> recipe_ingredient,
        -> ingredients
        -> where ingredient_id = ingredients.id
        -> group by  recipe_id, available) x, recipes
        -> where recipes.id = x.recipe_id
        -> group by x.recipe_id
        -> having count(*) = 1
        -> and max(x.available) = 'y';
    +---------------+
    | name          |
    +---------------+
    | cookie        |
    | soup          |
    | xtreme flavor |
    +---------------+
    3 rows in set (0.06 sec)

    mysql>
    mysql> update ingredients set available = 'n';
    Query OK, 7 rows affected (0.00 sec)
    Rows matched: 7  Changed: 7  Warnings: 0

    mysql>
    mysql> update ingredients set available = 'y'
        -> where id in  (1,7);
    Query OK, 2 rows affected (0.00 sec)
    Rows matched: 2  Changed: 2  Warnings: 0

    mysql>
    mysql> select recipes.name from
        -> (select recipe_id, available from
        -> recipe_ingredient,
        -> ingredients
        -> where ingredient_id = ingredients.id
        -> group by  recipe_id, available) x, recipes
        -> where recipes.id = x.recipe_id
        -> group by x.recipe_id
        -> having count(*) = 1
        -> and max(x.available) = 'y';
    +------+
    | name |
    +------+
    | soup |
    +------+
    1 row in set (0.06 sec)

    mysql>
    mysql>
    mysql> update ingredients set available = 'n';
    Query OK, 2 rows affected (0.00 sec)
    Rows matched: 7  Changed: 2  Warnings: 0

    mysql>
    mysql> update ingredients set available = 'y'
        -> where id in  (4,3);
    Query OK, 2 rows affected (0.00 sec)
    Rows matched: 2  Changed: 2  Warnings: 0

    mysql>
    mysql> select recipes.name from
        -> (select recipe_id, available from
        -> recipe_ingredient,
        -> ingredients
        -> where ingredient_id = ingredients.id
        -> group by  recipe_id, available) x, recipes
        -> where recipes.id = x.recipe_id
        -> group by x.recipe_id
        -> having count(*) = 1
        -> and max(x.available) = 'y';
    +---------------+
    | name          |
    +---------------+
    | xtreme flavor |
    +---------------+
    1 row in set (0.05 sec)

    mysql>
    mysql>
    mysql> update ingredients set available = 'n';
    Query OK, 3 rows affected (0.00 sec)
    Rows matched: 7  Changed: 3  Warnings: 0

    mysql>
    mysql> update ingredients set available = 'y'
        -> where id in  (1,3,7);
    Query OK, 3 rows affected (0.00 sec)
    Rows matched: 3  Changed: 3  Warnings: 0

    mysql>
    mysql> select recipes.name from
        -> (select recipe_id, available from
        -> recipe_ingredient,
        -> ingredients
        -> where ingredient_id = ingredients.id
        -> group by  recipe_id, available) x, recipes
        -> where recipes.id = x.recipe_id
        -> group by x.recipe_id
        -> having count(*) = 1
        -> and max(x.available) = 'y';
    +------+
    | name |
    +------+
    | soup |
    +------+
    1 row in set (0.06 sec)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top