从给定的成分中找出可能的食谱数量? SQL 查询
Find the number of recipes possible from given ingredients? SQL Query
我正在努力找出查询(实际上是空白)以获取可以从可用原料库存中创建的所有食谱。
创建的表是:
CREATE TABLE recipe(id int, name varchar(25));
CREATE TABLE ingredients(id int, name varchar(25), stock int);
CREATE TABLE recipeingredients(recipe_id int, ingredients.id int, amount int);
数据库中的记录应该是这样的
Recipe
ID | Name |
1 | Brown Bread |
2 | White Bread |
Ingredients
ID | Name | Stock |
1 | White Flour | 2 |
2 | Wheat Flour | 1 |
3 | Yeast | 17 |
4 | Water | 12 |
RecipeIngredients
RecipeID | IngredientID | Amount |
1 | 1 | 1 |
1 | 3 | 4 |
1 | 4 | 8 |
2 | 2 | 1 |
2 | 3 | 4 |
2 | 4 | 8 |
所以结果会是这样的
Name | Count |
White Bread | 1 |
Wheat Bread | 1 |
这可能很简单,但现在 SQL 很生疏。
select recipe
, min([count]) as 'count'
from (
select r.name as recipe
, i.name as ingredient
, i.stock / ri.amount as 'count'
from recipeingredients ri
inner join ingredients i on i.id = ri.ingredients_id
inner join recipe r on r.id = ri.recipe_id
) q
group by recipe
我正在努力找出查询(实际上是空白)以获取可以从可用原料库存中创建的所有食谱。
创建的表是:
CREATE TABLE recipe(id int, name varchar(25));
CREATE TABLE ingredients(id int, name varchar(25), stock int);
CREATE TABLE recipeingredients(recipe_id int, ingredients.id int, amount int);
数据库中的记录应该是这样的
Recipe
ID | Name |
1 | Brown Bread |
2 | White Bread |
Ingredients
ID | Name | Stock |
1 | White Flour | 2 |
2 | Wheat Flour | 1 |
3 | Yeast | 17 |
4 | Water | 12 |
RecipeIngredients
RecipeID | IngredientID | Amount |
1 | 1 | 1 |
1 | 3 | 4 |
1 | 4 | 8 |
2 | 2 | 1 |
2 | 3 | 4 |
2 | 4 | 8 |
所以结果会是这样的
Name | Count |
White Bread | 1 |
Wheat Bread | 1 |
这可能很简单,但现在 SQL 很生疏。
select recipe
, min([count]) as 'count'
from (
select r.name as recipe
, i.name as ingredient
, i.stock / ri.amount as 'count'
from recipeingredients ri
inner join ingredients i on i.id = ri.ingredients_id
inner join recipe r on r.id = ri.recipe_id
) q
group by recipe