繁体   English   中英

从给定的原料中找出可能的食谱数量? 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM