[英]Find the number of recipes possible from given ingredients? SQL Query
I'm struggling to figure out the query (drawing a blank really) to get all recipes that can be created from the stock of ingredients available.我正在努力找出查询(实际上是画一个空白)以获取可以从可用成分库存中创建的所有食谱。
Tables that are created are:创建的表有:
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);
Records in the database would be something like this数据库中的记录将是这样的
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 |
So the result would be something like this所以结果会是这样的
Name | Count |
White Bread | 1 |
Wheat Bread | 1 |
This might be something simple but am very rusty at SQL now.这可能很简单,但现在我对 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.