我有三张桌子。将它们视为以下内容:
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
希望你明白了。我想要的是一个查询,我可以找到所有食谱,这些食谱的成分是给定成分的一部分。
我的想法是,我希望能够列出我手边的所有东西(但当然不是我手边的所有东西。)
我尝试用不同级别的子查询和相关子查询与EXISTS实现这一点,但没有运气。我也尝试过使用HAVING和COUNT,但是如果我想要一些使用我手头所有成分的东西,这似乎对我有用。
答案 0 :(得分:1)
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)
答案 1 :(得分:1)
测试它是否有效。假设您在一个名为ingredients_avail的表中提供了(唯一的)成分ID,此查询应显示具有完整成分的配方ID:
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;
此外,这是一种更典型的语法:
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;